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

How to count how many players are on a team?

I am trying to count how many players are in a team, and then i'm going to put that amount of players in a number value in server storage. I'm using a script on a part only to test out if its working, but I don't think it is. Do you see any problems with it? (i'm a very beginner scripter)

this is the one i will use..

local players = game.Teams.Playing:GetPlayers()
game.ServerStorage.PlayerCount.Value = #players

and this is the one I made just to test if its working.

while true do
	if game.ServerStorage.PlayerCount.Value == 1 
	then
		script.Parent.Transparency = .5
	end
	wait(.00001)
end

(also im using one player because when I test it in the studio, its just one player on the playing team, me) Thank you in advance!

1 answer

✓ Accepted answer

Your code executes only once. You should update the value of the Number value whenever a player leaves/joins the Team. Here's how you'd do it

local ServerStorage = game:GetService("ServerStorage")
local Teams = game:GetService("Teams")

local function UpdateValue()
 ServerStorage.PlayerCount.Value = #Teams.Playing:GetPlayers()
end

Teams.Playing.PlayerAdded:Connect(UpdateValue)
Teams.Playing.PlayerRemoved:Connect(UpdateValue)

2 comments

0

Thank you so much, this really really helped!

0

I'm glad I could help!

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