Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make a team counter?

Asked by 10 years ago

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

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

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)
0
Thank you! truefire2 75 — 10y
Ad
Log in to vote
1
Answered by 10 years ago

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

0
Thank you. Unfortunately, I can't accept two answers. truefire2 75 — 10y
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.) juzzbyXfalcon 155 — 10y
Log in to vote
-1
Answered by
TomsGames 225 Moderation Voter
10 years ago

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
I know this. How will I make it so if a player changes teams so that the counter changes? truefire2 75 — 10y
Log in to vote
-1
Answered by
yob67 -10
10 years ago

Depends on team colors but Inspired is correct.

Answer this question