Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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?

0
You forgot an end User#20388 0 — 6y
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???? User#20388 0 — 6y
0
Yes i have values of names "String values" AnAnonymousDeveloper 77 — 6y
0
Is this your error message? "Argument 1 missing or nil" Then this, `game.Players:FindFirstChild(nil)` is happening. Do what cabbler wrote. MooMooThalahlah 421 — 6y
0
And change the if statement to `if players[i].Name == nil then` MooMooThalahlah 421 — 6y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
6 years ago

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
Ad

Answer this question