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 get this script that detects the number of players on a team to work?

So this script is supposed to print "Enough players" when both teams have enough players, and say "Not enough players" when neither team has enough players. However, the script is not working, and I don't know why. Can someone please help me?

Here's the script:

local team = game.Teams;

function getPlayers(teamColor)
    local found = { };
    for _,v in pairs(game.Players:GetPlayers()) do
        if v.TeamColor == teamColor then
            table.insert(found, v)
        end
    end
    return #found
end

while wait() do
getPlayers(BrickColor.new('Bright green')).Changed:connect(function()
getPlayers(BrickColor.new('Bright red')).Changed:connect(function()
if getPlayers(BrickColor.new('Bright green')) > 0 and getPlayers(BrickColor.new('Bright red')) > 0 then
print('Enough players')
elseif getPlayers(BrickColor.new('Bright green')) == 0 or getPlayers(BrickColor.new('Bright red')) == 0 then
print('Not enough players')
end
end)
end)
end

1 answer

You cannot use the Changed event on a custom function, it's used for values. Read more here

local team = game.Teams;

function getPlayers(teamColor)
	local found = { };
	for _,v in pairs(game.Players:GetPlayers()) do
		if v.TeamColor == teamColor then
			table.insert(found, v)
		end
	end
	return #found
end

while wait() do
	local greenNum = getPlayers(BrickColor.new('Bright green'))
	local redNum = getPlayers(BrickColor.new('Bright red'))
	if greenNum > 0 and redNum > 0 then
		print('Enough players')
	else
		print('Not enough players')
	end
end

3 comments

0

Would that code print a different message if the number of people on both teams changed?

0

If any of the teams' members went below 1 then it would print 'Not enough players'

0

But let's say that originally, one team had enough but the other didn't. It would print "Not enough players". But if the other team got enough players, would the script then print "Enough players"?

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