I have code like so, everything work, except for the fact that the table doesn't sort, which is obviously the most important part. Can anyone find out what wrong? I certainly can't, Any help would be much appreciated.
Yes the number I am sorting does get updated, I tried with both and it still return the same results...
local function SelectMap() local Votes = {{"BridgeBrigade", 0}, {"Map2", 0}} for _, Player in pairs(PlayersService:GetPlayers()) do if Player:FindFirstChild("Votes") then for Index, Value in pairs(Votes) do if Votes[Index][1] == Player.Votes.Map.Value then Votes[Index][2] = Votes[Index][2] + 1 end end end end table.sort(Votes, function(A, B) --This isn't sorting... print(A[1], B[1]) return A[2] < B[2] end) print(Votes[1][1]) end
[ UPDATE ]
So I just did some debug tests and apparently "Map2" is a nil value but the value in the same table isn't. That strange
There is no dictionary involved here. All of the tables you're using are lists.
I would recommend using dictionaries instead. Each Vote object should have a .Name
instead of [1]
and .Votes
instead of [2]
. It's much easier to read and understand that way, and less likely to make a mistake.
Your use of table.sort
is correct.
Have you added debugging into the loop to see if there are any players with a "Vote"
object and what in fact Player.Votes.Map.Value
is?
What do you see if you print
those things out? That's what I meant by debugging - if you actually debugged, you should have an idea what's right and what's wrong.
Lua has one form of compound-data-structure (a thing that has multiple pieces). That is called a table. There are two main ways to classify the way a table is used.
A list is an ordered collection of elements. There's a first thing: list[1]
, and a last thing: list[#list]
.
A list uses 1
, 2
, 3
, ..., as its indices.
A dictionary is an unordered collection of elements, where each element is labelled by a key.
For example, a dictionary could have a dic.Name
or dic["Name"]
or dic[mypart]
.
A dictionary is a table that isn't a list.