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

How do I get this script that detects the number of players on a team to work?

Asked by 9 years ago

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

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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
0
Would that code print a different message if the number of people on both teams changed? CoolJohnnyboy 121 — 9y
0
If any of the teams' members went below 1 then it would print 'Not enough players' Goulstem 8144 — 9y
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"? CoolJohnnyboy 121 — 9y
Ad

Answer this question