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
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?
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.
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.
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
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.
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.
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.
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
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