Ive made a round system that works fine, and also a custom “InGame” attribute to every player that joins, my only issue is that when i try to change it using the script below it does nothing, im not sure why it happens, tried doing some research but didnt find anything useful
local players = game:GetService("Players")
--iterate over each player in the game and update their attribute
for _, player in next, players:GetPlayers(), nil do
player:SetAttribute("InGame", true) --set the player's attribute
end
players:GetPlayers() returns a table of all the players in the game.
This is called an iteration, commonly known as a for loop. We’re looping over every player in the game. The variables _ and player are updated each pass of the iteration - they are updated for each player. _ holds the index the player is in the table returned by players:GetPlayers(). We don’t use this, it’s indicated by an underscore. The player is stored in the player variable for that pass of the iteration. The enclosed block of code within this loop is run for each player within the game.
next is just an iterator function - it’s called each pass of the loop and it’s what updates the _ and player variables.
ok, so it works but if someone joined in mid game, would he get ingame attribute set to true too or itd stay false? i think it should stay false if player joined in mid game
If you run this code at the start of the game, the player’s attribute will remain false.
Let’s take this structure:
All players’ attributes set to true
Game happens
All players’ attributes are set to false
And the scenario:
All players’ attributes are set to true
Game starts
Half way through, a player joins. Their attribute is set to false when they join because of your PlayerAdded code connected to the Players.PlayerAdded event. Since they weren’t in the game when all player’s attributes were set to true, theirs remains false.
Round finishes. All players’ attributes are set to false. Since this player’s attribute is already false, re-assigning it makes no difference.
Iterations will run when you tell them to, and they will run until they complete. If you tell it to run at the start of the round, it will finish before the player joins. If you tell it to run at the end of the round as well, the player will be included in it because it runs after they have joined.