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

My team picking script puts everyone on the same team??

Asked by 8 years ago

Okay so this is supposed to select a random player to be chosen for the red team and put the rest of the players on the other team.

var.players.Value is the number of players in the game.

it should select a random number out of the number of players and then should catch that number on the loop while letting the other users pass to the green team.

The problem is that it's putting everyone on the red team.

Thanks in advance for the help!


local spawnPlayers = function()

    local chosenPlayer = math.random(var.players.Value)

    for i,player in pairs(game.Players:GetChildren()) do

        if chosenPlayer == i then

            player.Character.Head:destroy()

            player.TeamColor = BrickColor.new('Bright red')

        else

            player.Character.Head:destroy()

            player.TeamColor = BrickColor.new('Medium green')

        end                

    end        

end

1 answer

Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
8 years ago

Instead of var.players.Value, just use NumPlayers.

Instead of Players:GetChildren(), use GetPlayers()

Capitilize :Destroy(), and move it outside of the if statement so we don't have to have it written twice.

Also, try using ipairs, so the table is always ran in the same order.

local spawnPlayers = function()

    local chosenPlayer = math.random(game.Players.NumPlayers)

    for index, player in ipairs(game.Players:GetPlayers()) do

    player.Character.Head:Destroy()

        if index == chosenPlayer then

            player.TeamColor = BrickColor.new('Bright red')

        else

            player.TeamColor = BrickColor.new('Medium green')

        end
    end
end

spawnPlayers()

Other than that, your script should be working.

0
Still having the same issue where they are both put on the same team... It doesn't make any sense at all! the script should work??? Thanks for the tip on NumPlayers though, I had no idea that existed! redpenchill 0 — 8y
Ad

Answer this question