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

How do I make a team where there is a limit to how many players are in there?

Asked by
Yeevivor4 155
8 years ago

Hello, thank you for reading. What I'm trying to do is make a team where there is a limit. What I'm trying to do is make a IntValue which adds +1 to itself each time a player is identified as a player who is on the prison guard team. It is not working and I don't know why.

The other problem is that once a player leaves the server, I want the script to recheck if the person who left was on the prisoner team. If he was, then the IntValue will be subtracted by 1.

If anyone can help me with these two problems, I'll be so grateful towards you.

GuardCount = game.Workspace.GuardCount
players = game.Players:getChildren()

function addGuards()
for i = 1, #players do
    if players[i].Character ~= nil then
        if players[i].TeamColor == game.Teams.PrisonGuard.TeamColor then
            GuardCount.Value = GuardCount.Value + 1
            end
        end
    end
end

game.Players.ChildAdded:connect(addGuards)
0
You will have to make the client check whether or not it is okay to enter the team. In addition, use PlayerAdded instead of ChildAdded for getting the players. Marios2 360 — 8y

1 answer

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

On line 2, you're getting all of the players. You do this once, right as the script is created, so you get zero players. This is never updated. On line 5, you're iterating through the list of players that were in the game when the script was created (the zero players.)

You want to move line 2 to before line 5.

local GuardCount=workspace.GuardCount
game.Players.PlayerAdded:connect(function()
    local players=game.Players:GetPlayers()
    for i=1,#players do
        if players[i].TeamColor=game.Teams.PrisonGuard.TeamColor then
            GuardCount.Value=GuardCount.Value+1
        end
    end
end)
Ad

Answer this question