Updating text to match player count on specific teams

Hey there!

Wanting to know how to consistently update the text in order to show up-to-date player count.
My code only shows what the script has already run but doesn’t repeat the same process if a new player has joined a team.

How would I go about doing this for my code?

Local script to change text to amount of players on a specific team:

local Teams = game:GetService("Teams")
local blueTeam = Teams.Blue
local redTeam = Teams.Red

function getTeamSize(team)
	return #team:GetPlayers()
end

local blueResult = getTeamSize(blueTeam)
local redResult = getTeamSize(redTeam)

local redCounter = script.Parent.RedCount
local blueCounter = script.Parent.BlueCount

redCounter.Text = redResult .. "/16"
blueCounter.Text = redResult .. "/16"

Image of script in studio

Thanks!
Kind regards,
ricefield_man.

2 Likes

You can add while - do loop.

Example:

  while wait() do
    local Teams = game:GetService("Teams")
    local blueTeam = Teams.Blue
    local redTeam = Teams.Red

function getTeamSize(team)
	return #team:GetPlayers()
end

local blueResult = getTeamSize(blueTeam)
local redResult = getTeamSize(redTeam)

local redCounter = script.Parent.RedCount
local blueCounter = script.Parent.BlueCount

redCounter.Text = redResult .. "/16"
blueCounter.Text = redResult .. "/16"
end
1 Like

You can use ChildAdded and ChildRemoved event, like this.

local Team = nil -- Your path here.

Team.ChildAdded:Connect(function(Instance)
      -- Whatever you'd do once a player joins the team.
end)

Team.ChildRemoved:Connect(function(Instance)
      -- Whatever you'd do once a player leaves the team.
end)
1 Like

Much appreciated @ShesSoCuteeee and @imAlex_30, thank you guys for helping me out!
I tried both of your methods and Alex’s seems to work best for me.

2 Likes