How do I make math.random not repeat in this case?

Hello! I can’t think of a way to prevent players from spawning on the same spawn every new round. Here is a bit of my code, if you guys need the whole code, I can send it later:

local function gameStart()
	local playersSetup = {}

	while true do
		for i,plr in pairs(Players:GetPlayers()) do
			local plrgui = plr.PlayerGui
			local userId = plr.UserId

			if table.find(queuedPlayers,userId) and not playersSetup[userId] then
				table.remove(queuedPlayers,table.find(queuedPlayers,userId))
				plr.Team = playingTeam
				playersSetup[userId] = true

				local char = plr.Character
				local hum = char.Humanoid
				local HRP = char.HumanoidRootPart
				
				local spawnNum = math.random(1,#spawns:GetChildren()) -- here is the random part
				
				local spawnpart = spawns[spawnNum]
							
				task.spawn(function()
					turnoffButtons()
					HRP.CFrame = spawnpart.CFrame + Vector3.new(0,5,0) -- here is where i assign  a player to the spawn
					spawnpart.CanCollide = true
					spawnpart.Transparency = 0
					hum.WalkSpeed = 0
					print(plrgui.Inventory.Frame.Visible)
					task.wait(1)
					updateText("3",true)
					task.wait(1)
					updateText("2",true)
					task.wait(1)
					updateText("1",true)
					task.wait(1)
					updateText("GO!",true)
					task.wait(0.5)
					updateText("",false)
					spawnpart.CanCollide = false
					spawnpart.Transparency = 1
					hum.WalkSpeed = 16
					plrgui:WaitForChild("AbilityGui").Enabled = true
					cooldownevent:FireAllClients(true,true)
				end)

The issue is you need to create a table for spawns then remove a spawn from the table to prevent players spawning in same spawns

Try this code:

local function gameStart()
	local playersSetup = {}
    local spawns=table.clone(spawns:GetChildren())
	while true do
		for i,plr in pairs(Players:GetPlayers()) do
			local plrgui = plr.PlayerGui
			local userId = plr.UserId

			if table.find(queuedPlayers,userId) and not playersSetup[userId] then
				table.remove(queuedPlayers,table.find(queuedPlayers,userId))
				plr.Team = playingTeam
				playersSetup[userId] = true

				local char = plr.Character
				local hum = char.Humanoid
				local HRP = char.HumanoidRootPart
				
				local spawnNum = math.random(1,#spawns) 
				
				local spawnpart = spawns[spawnNum]
							
				task.spawn(function()
					turnoffButtons()
					HRP.CFrame = spawnpart.CFrame + Vector3.new(0,5,0)
                    table.remove(spawns,spawnNum)
					spawnpart.CanCollide = true
					spawnpart.Transparency = 0
					hum.WalkSpeed = 0
					print(plrgui.Inventory.Frame.Visible)
					task.wait(1)
					updateText("3",true)
					task.wait(1)
					updateText("2",true)
					task.wait(1)
					updateText("1",true)
					task.wait(1)
					updateText("GO!",true)
					task.wait(0.5)
					updateText("",false)
					spawnpart.CanCollide = false
					spawnpart.Transparency = 1
					hum.WalkSpeed = 16
					plrgui:WaitForChild("AbilityGui").Enabled = true
					cooldownevent:FireAllClients(true,true)
				end)

you can store the old selected value outside the function, and then make sure that math.random doesn’t select that same value again

local storedSpawnNum

local function gameStart()
  ...
    local totalSpawns = spawns:GetChildren()

    if #totalSpawns > 1 then  
      -- This sets spawnNum to a random value until that value doesn't match the old one
      repeat spawnNum = math.random(1, #totalSpawns)
      until spawnNum ~= storedSpawnNum  
      storedSpawnNum = spawnNum  
    else 
      -- This sets spawnNum to 1 if the table has only 1 value contained inside
      spawnNum = 1  
    end  
  ...
end

edit: the code now can also work if the table has only 1 value. it could break without that “else” if the table had only 1 value


I have this issue, spawns is a folder btw


I have this problem with the spawnNum, i copied all of your code

add local spawnNum above local totalSpawns = spawns:GetChildren(), so it gets declared. i forgot to add it

1 Like

Thank you for your help! It works very well

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.