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

How can I fix this Lua script?

Asked by 9 years ago
local function getTeamPlayers(teamColor)
local output = {}
for _, player in pairs(game.Players:getPlayers()) do
if (not player.Neutral) and player.TeamColor == BrickColor.new(teamColor) then
table.insert(output, player)
end
end
return output
end

print(getTeamPlayers("Bright blue")) -->the number of players on the Bright blue team

function onClicked()
print "test" -- To test if it even gets up to this point
    if (getTeamPlayers("Bright blue")) < 17 then
        print(getTeamPlayers("Bright blue"))
        game.Players.LocalPlayer.TeamColor = BrickColor.new("Bright blue")
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

The above script does not work. It is supposed to find out the number of people on one team (The top area) and when a button is pressed use the top area to find out whether it can allocate the presser to the team (hence < 17). The thing is, it outputs a table, so how can I get the output to work with the bottom?

1 answer

Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

Your code outputs a table simply because function getTeamPlayers returns output, a table. In order to find the number of players on the team, you would have to use the length operator, which would return an integer indicating the number of entries in the table.

#getTeamPlayers() -- would return the number of players
--with on the Bright blue team.

It essentially works somewhat like this..


length = function( tab ) local count = 0 -- increases in accordance with --number of entries in provided table for index, value in next, tab do -- iterates through provided table count = count + 1 end return count -- returns total amount of found entries in table `tab` end length(getTeamPlayers())

solution

function onClicked()
print "test" -- To test if it even gets up to this point
    if (#getTeamPlayers("Bright blue")) < 17 then
        print(#getTeamPlayers("Bright blue"))
        game.Players.LocalPlayer.TeamColor = BrickColor.new("Bright blue")
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
0
I still hardly understand how I could integrate this into the original script. Can you edit your answer to make it whole Sharpthy 46 — 9y
0
Ok, I get how it works, but how do I go about including it into my original script? Sharpthy 46 — 9y
0
Already told you to use the '#' operator but yeah, I updated it with the solution ImageLabel 1541 — 9y
0
Thanks Sharpthy 46 — 9y
Ad

Answer this question