Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I use math.random?

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?

2 comments

0

Whoops just realised you wanted precentages...

0

Fixed it... I think it would work... (I hope) 0.0

1 answer

✓ Accepted answer
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

1 comment

0

Awesome, thanks! I am also unsure as to if this works, but it looks like it should. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.