Randomizing Team isn’t random after second run?

This randomizer randomizes the team your going to be only once, then you get the same result over and over. How do I fix this?


local rep = game:GetService("ReplicatedStorage")
local plrs = game:GetService("Players")
local votingSystem = game.Workspace.Voting
local status = rep.Status

local redTeam = game.Teams["Killer"]
local blueTeam = game.Teams["Runner"]
local blueTeamColor = BrickColor.new ("Lime green")
local redTeamColor = BrickColor.new ("Really red")

local lobbyTeam = game.Teams.Lobby

local choices = votingSystem:GetChildren()
local maps = rep.Maps:GetChildren()

local intermission = 10

local isAnOption
local randomMap

local chosenMap
local mapClone

local BlueTeamCount
local RedTeamCount


-- picking a random map function
local function PickRandomMap ()

	local randomNumber = math.random(1, #maps)

	randomMap = maps[randomNumber]

	return randomMap.CanBeVoted
end


for i, choice in pairs(choices) do

	local name = choice.label.SurfaceGui.TextLabel
	local picture = choice.Image.SurfaceGui.ImageLabel

	isAnOption = PickRandomMap()

	if isAnOption.Value == true then
		repeat 
			isAnOption = PickRandomMap()
		until
		isAnOption.Value == false
		name.Text = randomMap.Name
		picture.Image = randomMap.Image.Value
		randomMap.CanBeVoted.Value = true

	else
		name.Text = randomMap.Name
		picture.Image = randomMap.Image.Value
		randomMap.CanBeVoted.Value = true		
	end					
end	


rep.InRound.Changed:Connect(function()

	if rep.InRound.Value == false then

		mapClone:Destroy()

		for i, map in pairs(maps) do
			map.CanBeVoted.Value = false
		end

		for i, choice in pairs(choices) do

			local name = choice.label.SurfaceGui.TextLabel
			local picture = choice.Image.SurfaceGui.ImageLabel

			isAnOption = PickRandomMap()

			if isAnOption.Value == true then
				repeat 
					isAnOption = PickRandomMap()
				until
				isAnOption.Value == false
				name.Text = randomMap.Name
				picture.Image = randomMap.Image.Value
				randomMap.CanBeVoted.Value = true

			else
				name.Text = randomMap.Name
				picture.Image = randomMap.Image.Value
				randomMap.CanBeVoted.Value = true		
			end					
		end	


	else

		-- after the intermission has ended, the round will soon begin
		--- when the map with most votes will be spawned

		local Choice1Votes = #votingSystem.Choice1.button.Votes:GetChildren()
		local Choice2Votes = #votingSystem.Choice2.button.Votes:GetChildren()
		local Choice3Votes = #votingSystem.Choice3.button.Votes:GetChildren()

		if Choice1Votes >= Choice2Votes and Choice1Votes >= Choice3Votes then

			chosenMap = votingSystem.Choice1.label.SurfaceGui.TextLabel.Text

		elseif Choice2Votes >= Choice1Votes and Choice2Votes >= Choice3Votes then

			chosenMap = votingSystem.Choice2.label.SurfaceGui.TextLabel.Text

		else

			chosenMap = votingSystem.Choice3.label.SurfaceGui.TextLabel.Text

		end

		-- shows which map has won

		status.Value = "The Chosen map is: ".. chosenMap

		--- getting the map from the replicated storgae to the workspace

		for i, map in pairs(maps) do
			if chosenMap == map.Name then
				mapClone = map:Clone()
				mapClone.Parent = game.Workspace
			end
		end	

		--[[ another short delay right before the players get teleported you would also need to add 
		the same delay on the round system script]]
		wait(3)

		-- clears all the votes for next round

		for i, choice in pairs(choices) do
			choice.label.SurfaceGui.TextLabel.Text = " "
			choice.Image.SurfaceGui.ImageLabel.Image = " "
			choice.button.Votes:ClearAllChildren()
			rep.VoteReset:FireAllClients(choice.button)
		end

		-- this is if there are no teams wit dedicated spawns (free for all):

		--local spawns = mapClone.Spawns:GetChildren()

		local BlueTeamCount = {}
		local RedTeamCount = {}

		for i, plr in pairs(plrs:GetChildren()) do

			local char = plr.Character
			local humanRoot = char:WaitForChild("HumanoidRootPart")
			local nameGui = char.Head:FindFirstChild("NameGUI")

			local RedSpawns = mapClone.RedSpawns:GetChildren()
			local BlueSpawns = mapClone.BlueSpawns:GetChildren()

			-- picks a random spawn from each team
			local randomRedSpawn = RedSpawns[math.random(1,#RedSpawns)]
			local randomBlueSpawn = BlueSpawns[math.random(1,#BlueSpawns)]

			-- will put the current player into a team with the less amount of players

			if #RedTeamCount == 1 then

				plr.Team = blueTeam
				-- this is so we can make the spawn location for each player more randomized
				humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

				table.insert(BlueTeamCount, plr.Name)
				if nameGui then
					nameGui.name.TextColor3 = blueTeamColor
				end
				print(plr.Name .. " put in ".. plr.Team.Name)

			elseif #RedTeamCount == 0 then

				plr.Team = redTeam
				plr.CameraMode = "LockFirstPerson"
				humanRoot.CFrame = randomRedSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

				table.insert(RedTeamCount, plr.Name)

				if nameGui then
					nameGui.name.TextColor3 = redTeamColor
				end

				print(plr.Name .. " put in ".. plr.Team.Name)

				-- if both teams have the same amount of players, it choses a random team using math.random

			else

				local randTeam = Random.new(1,2)

				if randTeam == 1 then

					plr.Team = blueTeam
					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

					table.insert(BlueTeamCount, plr.Name)

					if nameGui then
						nameGui.name.TextColor3 = blueTeamColor
					end

					print(plr.Name .. " put in ".. plr.Team.Name)

				else

					plr.Team = blueTeam

					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

					table.insert(RedTeamCount, plr.Name)
					if nameGui then
						nameGui.name.TextColor3 = redTeamColor
					end
					print(plr.Name .. " put in ".. plr.Team.Name)

				end
			end



			--------- if the player dies, it puts them back in the lobby and removes them from the team

			-- when a player has died
			char:WaitForChild("Humanoid").Died:Connect(function()

				plr.Team = lobbyTeam


			end)

		end	

	end	
end)

This is line that is responsible for the random teams

local randTeam = Random.new(1,2)

How do I make it randomize everytime?

Instead of using Random:

local randTeam = Random.new(1,2)

use math.random:

local randTeam = math.random(1,2)

I changed it to Random.new because math.random did the same thing.

Try this:

--//Services
local rep = game:GetService("ReplicatedStorage")
local plrs = game:GetService("Players")
local Teams = game:GetService("Teams")

--//Variables
local votingSystem = workspace.Voting
local status = rep.Status
local lobbyTeam = Teams.Lobby
local redTeam = Teams.Killer
local blueTeam = Teams.Runner

--//Controls
local blueTeamColor = BrickColor.new("Lime green")
local redTeamColor = BrickColor.new("Really red")

local choices = votingSystem:GetChildren()
local maps = rep.Maps:GetChildren()

local intermission = 10

local isAnOption = nil
local randomMap = nil

local chosenMap = nil
local mapClone = nil

local BlueTeamCount = nil
local RedTeamCount = nil

local previousNumber = nil

--//Functions
local function GetRandomNumber()
	local randomNumber = Random.new():NextInteger(1, #maps)
	
	if randomNumber == previousNumber then
		repeat
			randomNumber = Random.new():NextInteger(1, #maps)
		until randomNumber ~= previousNumber
	end
	
	previousNumber = randomNumber
	return randomNumber
end

local function PickRandomMap()
	local randomNumber = GetRandomNumber()

	randomMap = maps[randomNumber]

	return randomMap.CanBeVoted
end

for i, choice in pairs(choices) do
	local name = choice.label.SurfaceGui.TextLabel
	local picture = choice.Image.SurfaceGui.ImageLabel

	isAnOption = PickRandomMap()

	if isAnOption.Value then
		repeat 
			isAnOption = PickRandomMap()
		until not isAnOption.Value
		
		name.Text = randomMap.Name
		picture.Image = randomMap.Image.Value
		randomMap.CanBeVoted.Value = true

	else
		name.Text = randomMap.Name
		picture.Image = randomMap.Image.Value
		randomMap.CanBeVoted.Value = true		
	end					
end

rep.InRound.Changed:Connect(function()
	if not rep.InRound.Value then
		mapClone:Destroy()

		for i, map in pairs(maps) do
			map.CanBeVoted.Value = false
		end

		for i, choice in pairs(choices) do
			local name = choice.label.SurfaceGui.TextLabel
			local picture = choice.Image.SurfaceGui.ImageLabel

			isAnOption = PickRandomMap()

			if isAnOption.Value == true then
				repeat 
					isAnOption = PickRandomMap()
				until
				isAnOption.Value == false
				name.Text = randomMap.Name
				picture.Image = randomMap.Image.Value
				randomMap.CanBeVoted.Value = true

			else
				name.Text = randomMap.Name
				picture.Image = randomMap.Image.Value
				randomMap.CanBeVoted.Value = true		
			end					
		end	
	else
		-- after the intermission has ended, the round will soon begin
		--- when the map with most votes will be spawned
		local Choice1Votes = #votingSystem.Choice1.button.Votes:GetChildren()
		local Choice2Votes = #votingSystem.Choice2.button.Votes:GetChildren()
		local Choice3Votes = #votingSystem.Choice3.button.Votes:GetChildren()

		if Choice1Votes >= Choice2Votes and Choice1Votes >= Choice3Votes then
			chosenMap = votingSystem.Choice1.label.SurfaceGui.TextLabel.Text
		elseif Choice2Votes >= Choice1Votes and Choice2Votes >= Choice3Votes then
			chosenMap = votingSystem.Choice2.label.SurfaceGui.TextLabel.Text
		else
			chosenMap = votingSystem.Choice3.label.SurfaceGui.TextLabel.Text
		end

		-- shows which map has won

		status.Value = "The Chosen map is: ".. chosenMap

		--- getting the map from the replicated storgae to the workspace

		for i, map in pairs(maps) do
			if chosenMap == map.Name then
				mapClone = map:Clone()
				mapClone.Parent = game.Workspace
			end
		end	

		--[[ another short delay right before the players get teleported you would also need to add 
		the same delay on the round system script]]
		task.wait(3)

		-- clears all the votes for next round

		for i, choice in pairs(choices) do
			choice.label.SurfaceGui.TextLabel.Text = " "
			choice.Image.SurfaceGui.ImageLabel.Image = " "
			choice.button.Votes:ClearAllChildren()
			rep.VoteReset:FireAllClients(choice.button)
		end

		-- this is if there are no teams wit dedicated spawns (free for all):

		--local spawns = mapClone.Spawns:GetChildren()

		local BlueTeamCount = {}
		local RedTeamCount = {}

		for i, plr in pairs(plrs:GetPlayers()) do
			local char = plr.Character
			local humanRoot = char:WaitForChild("HumanoidRootPart")
			local nameGui = char.Head:FindFirstChild("NameGUI")

			local RedSpawns = mapClone.RedSpawns:GetChildren()
			local BlueSpawns = mapClone.BlueSpawns:GetChildren()

			-- picks a random spawn from each team
			local randomRedSpawn = RedSpawns[math.random(1,#RedSpawns)]
			local randomBlueSpawn = BlueSpawns[math.random(1,#BlueSpawns)]

			-- will put the current player into a team with the less amount of players
			if #RedTeamCount == 1 then
				plr.Team = blueTeam
				-- this is so we can make the spawn location for each player more randomized
				humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

				table.insert(BlueTeamCount, plr.Name)
				
				if nameGui then
					nameGui.name.TextColor3 = blueTeamColor
				end
				
				print(plr.Name .. " put in ".. plr.Team.Name)

			elseif #RedTeamCount == 0 then
				plr.Team = redTeam
				plr.CameraMode = "LockFirstPerson"
				humanRoot.CFrame = randomRedSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

				table.insert(RedTeamCount, plr.Name)

				if nameGui then
					nameGui.name.TextColor3 = redTeamColor
				end

				print(plr.Name .. " put in ".. plr.Team.Name)
				-- if both teams have the same amount of players, it choses a random team using math.random

			else
				local randTeam = Random.new():NextInteger(1, 2)

				if randTeam == 1 then
					plr.Team = blueTeam
					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

					table.insert(BlueTeamCount, plr.Name)

					if nameGui then
						nameGui.name.TextColor3 = blueTeamColor
					end

					print(plr.Name .. " put in ".. plr.Team.Name)
				else
					plr.Team = blueTeam

					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

					table.insert(RedTeamCount, plr.Name)
					if nameGui then
						nameGui.name.TextColor3 = redTeamColor
					end
					print(plr.Name .. " put in ".. plr.Team.Name)
				end
			end

			--------- if the player dies, it puts them back in the lobby and removes them from the team

			-- when a player has died
			char:WaitForChild("Humanoid").Died:Connect(function()
				plr.Team = lobbyTeam
			end)
		end	
	end	
end)

For Random.new(), it has to be like this:

local randomNumber = Random.new():NextInteger(number1, number2)

Are you sure it affects the outcome of the second run?

It does but another thing that affects it is when you just randomized the numbers to pick the map without checking if it isn’t the same as the previous map. I already did that for you so just try my script.

1 Like

I don’t need to randomize the maps because players can vote for maps, thank you very much for the help (: