I was trying to use table.sort to sort int scores but It gave me this error : MainScript:84: attempt to index number with number. I have tried looking at other posts that explained table.sort but I could not find a clear enough explanation.
Lines 83 to 85 are below which is where the error occurred.
table.sort(buildscores, function(i, j)
return i[1] >= j[1]
end)
The 2D array I am using for the scores is formated like this:
local buildscores = {
{intscore, player}
{intscore, player}}
AustnBlox
(Austin)
October 18, 2022, 6:48am
#2
This might help as its a little similar to your topic:
You lack braces around your sub-tables.
local Table = {"Mag", 2, "Mag", 3, "Mag", 1}
This is what your table looks like right now, so each element is being iterated separately. For desired behavior, what you want is {{"Mag", 2}, {"Mag", 3}, {"Mag", 1}}.
Also, table.sort sorts the table in place and does not return anything, so you shouldn’t have to do local Sorted =.
Doing this works for me
local buildscores = {
{5, "you"},
{7, "me"},
{1, "someone else"}
}
table.sort(buildscores, function(i, j)
return i[1] >= j[1]
end)
print(buildscores)
Could you show more of your code?
It could be that buildscores
is actually just an array of numbers. (or what AustnBlox linked to)
2 Likes