How can I change custom attributes inside player for everyone in script?

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

game.Players:GetPlayers():GetAttribute("InGame", true)

(btw this is my script to setattribute)

game.Players.PlayerAdded:Connect(function(plr)
	plr:SetAttribute("InGame", false)
end)

so if you know smth let me know down below

2 Likes
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.
3 Likes

It’s because getplayers returns an array. So you have to loop through each player in the array. and set the attribute.

And also, you are using getattribute which only returns it instead of changing it.

To change an attribute you have to use setattribute.

local players = game:GetService("Players")

for _, player in players:GetPlayers() do
player:SetAttrbitue("InGame", true")
end

oh my god thanks so much, idk where id be without you

1 Like

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.

thanks buddy, i was just making sure

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.