Why doesn't this work?
local players = script.Parent.Parent.Parent.PlayerNames:GetChildren() local AllPlayersTeleported = false local NumberOfPlayersTeleported = 0 for i = 1, #players do if game.Players:FindFirstChild(players[i].Name) == nil then NumberOfPlayersTeleported = NumberOfPlayersTeleported + 1 table.remove(players, i) end end
Basically this line of code:
if game.Players:FindFirstChild(players[i].Name) == nil then
Which doesn't make sense because it returns the error that it is trying to index a nil value. I thought it's supposed to index a new value right? so why does the script break instead of return a nil value?
Common problem actually: table.remove
shifts around the array values, so your for-loop becomes inaccurate. Say for two players: index 1 should work, but removal replaces value1 with value2. Index 2 is unexpectedly nil.
There are at least 4 solution to this but I reccomend the easy Lua one: avoid table.remove. It's a slow function anyway.
NumberOfPlayersTeleported = NumberOfPlayersTeleported + 1 players[i] = nil