Posted 20 August 2019 - 05:07 AM
I have a shortened version of my code, but it does the same weirdness. I have table (t) that only has one item in it, which is an array. After I run it through highScore() it changes to 0 even though I only reference it as tab, then again through pTab. The goal of highScore() is to return an list of all items in t in order by score. Why is this happening?
local function highScore(tab, spot)
local pTab = tab
local scores = {}
local winner
while #pTab > 0 do
winner = {key = 0, score = 0}
for k,v in pairs(pTab) do
-- print(v.score)
if v.score > winner.score then
winner = {key = k, score = v.score}
end
end
scores[#scores+1] = pTab[winner.key]
table.remove(pTab, winner.key)
end
return scores
end
t = {
{
player = "TEST",
score = 0,
},
}
print(#t) -- prints 1 as it should
h = highScore(t)
print(#t) -- prints 0 when it should still be 1