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

How do I use math.random?

Asked by 10 years ago

I'm trying to randomly select a percentage of players to be in one team. I want 20% of the people to randomly be selected for the Red team, 60% to be selected for the Green team, and 10% to be selected for the Blue team.

This is what I have so far, does it work?

players = game.Players:GetChildren()
redpercentage = math.floor(#players * 0.2)
redteam = math.random(redpercentage, players)
greenpercentage = math.floor(#players * 0.6)
greenteam = math.random(greenpercentage, players)
bluepercentage = math.floor(#players * 0.1)
blueteam = math.random(bluepercentage, players)

Also, how could I, persay, select them, so that I could set their team color, give them certain Guis, and give them certain tools, based on their selected color?

0
Whoops just realised you wanted precentages... ObscureEntity 294 — 10y
0
Fixed it... I think it would work... (I hope) 0.0 ObscureEntity 294 — 10y

1 answer

Log in to vote
1
Answered by 10 years ago
local redTeam = {}
local greenTeam = {}
local blueTeam = {}

for a,b in pairs(game.Players:GetChildren()) do 
    local chance = math.random(1,100)
    if chance <= 0 then
        table.insert(redTeam,b)
    elseif chance <= 40 then
        table.insert(greenTeam,b)
    elseif chance <= 60 then
        table.insert(blueTeam,b)
    end
end

I don't know if this was helpful, This is just how I'd randomly insert players into tables(then insert them into teams). You can then do this:

for i = 1,#redTeam do
    local gui = game.Lighting.Gui:Clone()
    gui.Parent = redTeam[i].PlayerGui
end

and insert Guis or weapons like that.

Hope this helped:

~Sweetepa11fir

0
Awesome, thanks! I am also unsure as to if this works, but it looks like it should. Thanks! SlickPwner 534 — 10y
Ad

Answer this question