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

Why doesnt my script keep generating random teams?

Asked by
DeepDev 101
6 years ago

I've tested this piece of script with 5 players in a local server and they keeping getting on the same team while I want them to be even and randomized. Here's a fragment of my script:

local teams = {"Maroon","Institutional white"}
local players= game.Players:GetChildren()
local randomteam= math.random(1,#teams)

    for i,v in pairs(teams) do
        if randomteam==i then
    for u,d in pairs(players) do
        d.TeamColor= BrickColor.new(v)
        end
    end
end

They end up all in Maroon and I want some to be at Institutional White.

1 answer

Log in to vote
0
Answered by 6 years ago

Hello DeepDev,

The reason your teams are not actually random is caused by the fact that you are calling a random number in a variable, which means it can't change anymore. This below script should work:

local teams = {"Maroon", "Institutional white"}
local player = game.Players:GetChildren()

for i, v in pairs(teams) do
    wait() -- Always give a random number some time for them to become actually random ;)
    local randomteam = math.random(1, #teams)
    if randomteam == i then
        for u, d in pairs(players) do
            d.TeamColor = BrickColor.new(v)
        end
    end
end

I hope this answered your question, have fun and happy scripting!

Regards,

DefaultAxis

Ad

Answer this question