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