Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make a team counter?

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

4 answers

✓ Accepted answer

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)

1 comment

0

Thank you!

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

2 comments

0

Thank you. Unfortunately, I can't accept two answers.

0

That's all right, it would be appreciated if you could up-vote my answer though (Not sure if that is possible if you already chose an answer.)

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 :)

1 comment

1

I know this. How will I make it so if a player changes teams so that the counter changes?

This discussion is preserved as it was. New answers, comments, and votes are closed.