Hi. How would I make a dynamic GUI team counter which changes when a player leaves, dies/resets, changes teams or joins?
I don't know how to use the Changed event in order to change the counter number. Do I use the PlayerRemoving Event?
I got this in mind but I don't think it'll work.
while wait() do for i, v in next, game.Players:GetPlayers() do if v.TeamColor == "Bright red" then script.Parent.reds.Value = script.Parent.reds.Value + 1 elseif v.TeamColor == "Bright blue" then script.Parent.blues.Value = script.Parent.blues.Value+1 elseif v.TeamColor == "White" then script.Parent.whites.Value = script.Parent.whites.Value + 1 end end end
Since you are going to be listening to multiple events at once, I suggest making two functions, as so:
(By the way, you can use the tab character when entering code here. :)
local reds = script.Parent.reds local whites = script.Parent.whites local blues = script.Parent.blues function incrementTeamCounter(team) if team == BrickColor.new("Bright red") then reds.Value = reds.Value + 1 elseif team == BrickColor.new("Bright blue") then blues.Value = blues.Value + 1 else whites.Value = whites.Value + 1 end end function decrementTeamCounter(team) if team == BrickColor.new("Bright red") then reds.Value = reds.Value - 1 elseif team == BrickColor.new("Bright blue") then blues.Value = blues.Value - 1 else whites.Value = whites.Value - 1 end end
And now, you just have to connect everything to these functions!
Game.Players.PlayerAdded:connect(player) --Player joins incrementTeamCounter(player.TeamColor) player.CharacterAdded:connect(character) character.Humanoid.Died:connect(function() --Player dies decrementTeamCounter(player.TeamColor) end) end) local currentTeamColor = player.TeamColor player.Changed:connect(function(prop) if prop == player.TeamColor then --Team changes decrementTeamCounter(currentTeamColor) incrementTeamCounter(player.TeamColor) currentTeamColor = player.TeamColor end end) end) Game.Players.PlayerRemoving:connect(player) --Player leaves decrementTeamcounter(player.TeamColor) end)
Beforehand: sorry for posting a new answer. This system is not allowing me to make comments to other people's answers >.>
I would much prefer to have this (if necessary, global) function:
function Check() for _,Player in pairs (game.Players:GetPlayers()) do if v.TeamColor == "Bright red" then script.Parent.reds.Value = script.Parent.reds.Value + 1 elseif v.TeamColor == "Bright blue" then script.Parent.blues.Value = script.Parent.blues.Value+1 elseif v.TeamColor == "White" then script.Parent.whites.Value = script.Parent.whites.Value + 1 end end end
And then have these events:
game.Players.PlayerAdded:connect(function(Player) Check() Player.Changed:connect(function() Check() end) Player.CharacterAdded:connect(function() Check() end) end) game.Players.PlayerRemoving:connect(function() Check() end)
Although, I see no use for having the CharacterAdded event in here. Dying has nothing to do with changing team and if you had a different script that does change the team on death the .Changed event will fire
Make a function which changes a specific value, then stick a listening function at the end.
For example, game.Players.PlayerAdded:connect(addaplayer)
addaplayer is a function which increases the Gui's player count by 1.
That's an example, you can probably figure out the rest with this little bit of guidance :)