So, I have a for loop that is suppose to get a list of all the players in the game using GetPlayers(), as shown here:
local PlayerList = {} do for _,player in pairs(game.Players:GetPlayers()) do if player:IsA("Player") then PlayerList[player] = true end end PlayerAmount = (#PlayerList) end
However something wasn't working later in the code when I needed PlayerAmount, so for debugging I added this:
print(PlayerAmount)
And it prints 0. Anyone have any idea whats wrong? Any help is appreciated, thanks! :)
And no, it's not disabled. :)
You can't use #
to get the correct number of elements of a table unless:
t = {["this"] = "is", ["a"] = "dictionary"} print(#t) -- prints 0 t = {[2] = "b"} print(#t) -- prints 0 t = {[1] = "a", [2] = nil} print(#t) -- prints 1 t = {[1] = "a", [3] = "c"} print(#t) -- prints 1 t = {[1] = "a", [2] = "b", [3] = "c"} print(#t) -- prints 3
Instead of doing PlayerList[player] = true
, just do table.insert(PlayerList, player)
. That way, all indices are guaranteed to be sequential (unless, of course, you're inserting nil).