Rounds system teleporting not working

im making a round system where it waits for the timer to end then it picks one killer then the rest of the players are survivors, the code for picking a random spawn location won’t work, this is my code

if script.Parent.Time.Value == 0 then
	if workspace.inRound.Value == false then
		workspace.inRound.Value = true
		script.Parent.Time.Value = 240
		local players = game:GetService("Players"):GetPlayers()
		local killer = players[math.random(1, #players)]
		killer.Character.killer.Value = true
		local folder = workspace["Killer Spawns"]:GetChildren() --getting killer spawn parts
		local Killerspawn = folder[math.random(1, #folder)] --picking a random killer spawn
		killer.Character.HumanoidRootPart.CFrame = Killerspawn.CFrame * CFrame.new(0, 4, 0) --teleport killer to location
		print(killer)
		if playerCount > 1 then
			for i = #players, 0, -1 do
				if players[i].Name == killer.Name then return end
				local player = players[i]
				player.Character.survivor.Value = true
				local folder2 = workspace["Survivor spawns"]:GetChildren() --getting survivor spawn parts
				local Survivorspawn = folder2[math.random(1, #folder2)] --picking a survivor spawn
				player.Character.HumanoidRootPart.CFrame = Survivorspawn.CFrame * CFrame.new(0, 4, 0) --teleport survivor to location
			end
		end
	else
		workspace.inRound.Value = false
		script.Parent.Time.Value = 50
	end
end

it keeps saying “Workspace.Rounds:27: invalid argument #2 to ‘random’ (interval is empty)” for when it tries to choose a random spawn

Which one is line 27? I’m assuming that there’s more lines of code before this if statement

Wait I think it’s your for loop. Try changing it to

for i = #players, 1, -1 do

Whenever you play the game, I assume this runs RIGHT away. Your player has NOT loaded into the game yet most likely. So this line here

local killer = players[math.random(1, #players)]

Is doing between 1 and 0, and that is not possible. So you need to check if there is at LEAST 2 players, one for the killer, and one for the other players.

So swap your code to something like this:

if script.Parent.Time.Value == 0 then
	if workspace.inRound.Value == false then

-- Waits for at least 2 players before starting
repeat
task.wait(1)
until #game:GetService("Players"):GetPlayers() >= 2

		workspace.inRound.Value = true
		script.Parent.Time.Value = 240
		local players = game:GetService("Players"):GetPlayers()
		local killer = players[math.random(1, #players)]
		killer.Character.killer.Value = true
		local folder = workspace["Killer Spawns"]:GetChildren() --getting killer spawn parts
		local Killerspawn = folder[math.random(1, #folder)] --picking a random killer spawn
		killer.Character.HumanoidRootPart.CFrame = Killerspawn.CFrame * CFrame.new(0, 4, 0) --teleport killer to location
		print(killer)
		if playerCount > 1 then
			for i = #players, 0, -1 do
				if players[i].Name == killer.Name then return end
				local player = players[i]
				player.Character.survivor.Value = true
				local folder2 = workspace["Survivor spawns"]:GetChildren() --getting survivor spawn parts
				local Survivorspawn = folder2[math.random(1, #folder2)] --picking a survivor spawn
				player.Character.HumanoidRootPart.CFrame = Survivorspawn.CFrame * CFrame.new(0, 4, 0) --teleport survivor to location
			end
		end
	else
		workspace.inRound.Value = false
		script.Parent.Time.Value = 50
	end
end
1 Like

I fixed it already with some help

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