If statement stoping a loop

I am currently trying to add every player in the game in a current state to a array. I have made a loop to add the players to and array. Whenever the script runs, the first person on the leaderboard doesn’t get anyone put into the array. I have tried using if else as well as pairs and ipairs. Here is the loop:

local SpecList = {}
local Players = game.Players:GetPlayers()
for i, v in ipairs(Players) do
	local Value = v.Character:WaitForChild("Value").Value
	if Value ~= 2 and v.Name ~= Player.Name then
		table.insert(SpecList, v)
	end
end
2 Likes

I don’t understand what this line means, but everything else seems to be good!

Everything else appears to be fine, but I believe the reason why you are having these issues is because the players don’t meet the criteria.

Can you explain what the Value means? Maybe the first person on the leaderboard has a value of 2 in their character.
Also, what is the Player.Name? I can’t help you further with that because I don’t see anything for Player being defined. Perhaps the Player is the same as the person on the top of the leaderboard?

Thank you!

1 Like

The value is a number value in the player’s character. The Player variable is the character of the player that the given script is in. The script is a local script in a gui.

Ah! I understand now.

So, if I understand it, you want the index to have everyone EXCEPT for the LocalPlayer (the person who is running the script). Can you confirm that?

If that is correct, and you don’t want the LocalPlayer to be in the table, then the problem lies with the value. If the Value is 2 or the player is the LocalPlayer, then it will NOT add it to the table.

Hope this helps

1 Like

Yes, that is exactly what I am trying to do.

I believe that the if statement is causing the loop to end before it has gone through every player though.

Is the value for those who are currently in spectator mode or the local player specifically? If the latter and this is a local script, you could use LocalPlayer.

Just use FindFirstChild instead

local SpectatorMode = v.Character:FindFirstChild("Value")
if SpectatorMode ~= nil and SpectatorMode.Value ~= 2 and v.Name ~= Player.Name then
1 Like
local Value = v.Character:WaitForChild("Value").Value

don’t just use this line. if a player just died, then it doesn’t have a character and the script will error for trying to index nil with :WaitForChild. so instead, first make sure player.Character isn’t nil.

1 Like

The value can detect if the player is alive, close to death, or spectating. The value is in each player. I tried to use FindFirstChild() but that didn’t work. There is only one child of the character named “Value.” Also, I have character auto loads turned off so I don’t have to spawn the player in a box if they’re spectating.

1 Like

Try out that little code change and see what’ll happen.

1 Like

The issue that happens when I try that change is that no player is ever added to the array.

1 Like

print something outside the if statement and inside it. do they all print?

1 Like

I did that and both of them printed for player2. Only the one outside the if statement printed for player1.

1 Like

so print the Value variable outside the if statement, it might be 2

1 Like

Both printed 0. I think the issue is that something with the condition being false with the name is causing the loop to just end rather than continue to the next person.

1 Like

I guess you can use task.spawn to make sure it doesn’t stop by a person:

for i, v in ipairs(Players) do
    task.spawn(function()
	    local Value = v.Character:WaitForChild("Value").Value
	    if Value ~= 2 and v.Name ~= Player.Name then
	    	table.insert(SpecList, v)
  	    end
    end)
end
1 Like

Nothing changed when I tried that.

The loop cannot end by the hand of an if-statement alone. You need to explicitly end the loop with a return or break. If you’re not observing any results from the loop, then it’s either than your if-statement fails every iteration, or that the values the loop is iterating over are not what you think. For example, if you’re running this loop as soon as the server starts, it’s extremely likely that there are little to no player’s currently under the Players service, which would lead to your Players:GetPlayers call returning little to no players for processing. If this is done via a LocalScript, the code you’ve given us does not appear to run more than once, so the first player to enter the game would not have any other players to process

That would make sense with what is happening but how would I fix it? I tested it and that appears to be what is happening.

just use Players.PlayerAdded to update the table