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

Why doesnt my script keep generating random teams?

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

✓ Accepted answer

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

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