Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

if game.Players:FindFirstChild(players[i].Name) == nil then?

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?

5 comments

0

You forgot an end

0

local players = script.Parent.Parent.Parent.PlayerNames:GetChildren() what does that mean? Did you put string values in there or is it players or????

0

Yes i have values of names "String values"

0

Is this your error message? "Argument 1 missing or nil" Then this, game.Players:FindFirstChild(nil) is happening. Do what cabbler wrote.

0

And change the if statement to if players[i].Name == nil then

1 answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.