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

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

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)

1 comment

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.

1 answer

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)

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