I need help tying to figure out how to make my script constantly check how many players are on the team

So currently this script checks how many players are on a team and lists it on a text GUI. The problem is, if someone changes their team, it doesn’t update until the player resets their character (Reloads) the GUI. Is there a way to make it constantly check every couple seconds?

1 Like

Put it in a while true loop and add a wait to it.

1 Like

Can’t you just make 2 values and set up a changed event to update the number? Also use events to change the GUI to everyone

1 Like

Okay! The reason for this is the script only updates it one time, then stops!

You could technically do a while wait() do, or a run service loop, but that stresses the server out too much.

So here’s an updated version of your script:

local red = 0 -- red team
local blue = 0 -- Blue team

local function addPlayerToList(team)
if team == "red" then
	red += 1
elseif team == "blue" then
	blue += 1
end
script.Parent.PlayerNumber.Text = "( "..blue.." )"
end

local function removePlayerFromList(team)
if team == "red" then
	red -= 1
elseif team == "blue" then
	blue -= 1
end
script.Parent.PlayerNumber.Text = "( "..blue.." )"
end

game:GetService("Teams").Red.PlayerAdded:Connect(addPlayerToList("red")) -- Update team RED count.
game:GetService("Teams").Red.PlayerRemoved:Connect(addPlayerToList("red")) -- Update it again..

game:GetService("Teams").Blue.PlayerAdded:Connect(addPlayerToList("blue")) -- Now update BLUE team..
game:GetService("Teams").Blue.PlayerRemoved:Connect(addPlayerToList("blue")) -- same here!

This way, It only updates when a player is added/removed from said team!
(I hoped this helped lol.)

1 Like

There is no need to make a counter and increment it for each player.

local teams = game:GetService("Teams")
local playerCount = #teams.Red:GetPlayers() -- assuming that the team name is Red
print(playerCount)

This would be the easiest way to do it, you can easily loop it or listen to signals

3 Likes

Can’t you do something like this?

TeamNumber.Changed:Connect(function()
-- code to update
end)
1 Like

You can do something like this instead of looping.

local amount = #team:GetPlayers() -- replace team with your team object

-- change text
1 Like