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

How to count how many players are on a team?

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
Thank you so much, this really really helped! H0ney_Beeee 8 — 3y
0
I'm glad I could help! MalfestLuxor 35 — 3y
Ad

Answer this question