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

How to get random teams from a function?

Asked by 7 years ago
function getTeams()
    local teamNum = 1
    repeat wait()       
        local spectators = {}
        local teams = {"Red", "Blue"}
        local players = game.Players:GetChildren()
        for index, player in pairs(players) do
            if (player.TeamColor == game.Teams["Spectators"].TeamColor) then
                if (player.Character:findFirstChild("Torso")) then
                    table.insert(spectators, player)
                end
            end
        end
        local randomPlayer = spectators[math.random(1, #spectators)]
        randomPlayer.TeamColor = game.Teams[teams[teamNum]].TeamColor
        local spectatorNum = 0
        local players = game.Players:GetChildren()
        for index, player in pairs(players) do
            if (player.TeamColor == game.Teams["Spectators"].TeamColor) then
                if (player.Character:findFirstChild("Torso")) then
                    spectatorNum = spectatorNum + 1
                end
            end
        end
        teamNum = (teamNum == 1) and 2 or 1
    until (spectatorNum == 0)
end

What I have here is a function which gives a player a random team. The problem is that they aren't random. Every single person who joins is put onto red team. The point of this script being so long and complex is to make it so if someone joins halfway through a game, they don't cause everyone else to have their teams changed.

0
have you tried an on player entered script instead? as soon as they enter assign them to a random team, then you dont have to worry about the player messing up the other teams. That or your problem could be you're not using math.randomseed(Tick()) < copy that line of code and put it towards the beginning of your script just past the locals. Your math.random might not be random enough. lol ace12345678135 50 — 7y

Answer this question