RolePicker, teleport when round starts

Hi, im making a game were there is one killer and the rest is survivors. When the Intermission goes down to 0 i wanted all the survivors to teleport to the map but the killer will teleport to his waiting room. And when the rond ends everyone teleport back to the lobby.

The problem is that when the intermission turns 0 nothing happends but the round timer starts to go down.

I have spent some days now searching online and watching tutorials but i cant find a solution.

I started to learn scripting about a month ago so im pretty new to this. I hope someone can help me fix this so i can continue making my game. Thanks.

Here is my script

--Killer/Survivor Picker

local killer = nil
local survivors = {}

function PickPlayers()
	
	survivors = {}
	
	local plrs = game.Players:GetChildren()
	local KillerID = math.random(1, #plrs)

	for i, v in pairs(plrs) do
		if i == KillerID then
			killer = v.Name
			break
		end
	end
		
	for i, v in pairs(plrs) do
		if i == KillerID then
			
		else
			table.insert(survivors, v.Name)
		end
	end
	
	print("Killer: ".. killer)
	for i, v in pairs(survivors) do
		print("Survivor: ".. v)
		
	end
end
while wait(5) do
	PickPlayers()
	break
end

--Intermission, Roundtimer and Teleport

local roundLength = 100
local intermissionLength = 10
local InRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status

local LobbySpawn = game.Workspace.Lobby.Spawns.LobbySpawn
local GameAreaSpawn = game.Workspace.Hallway.Spawns.MapSpawn
local KillerAreaSpawn = game.Workspace.WaitingRoom.KillerWaitingSpawn

--Teleport

InRound.Changed:Connect(function()
	if InRound.Value == true then
		for i, v in pairs(survivors) do
			local char = v.Character
			char.HumanoidRootPart.CFrame = GameAreaSpawn.CFrame
		end
	else
		for i, v in pairs(killer) do
			local char = v.Character
			char.HumanoidRootPart.CFrame = KillerAreaSpawn.CFrame
		end	
	end
end)


InRound.Changed:Connect(function()
	if InRound.Value == false then
		for i, v in pairs(game.Players:GetChildren()) do
			local char = v.Character
			char.HumanoidRootPart.CFrame = LobbySpawn.CFrame
		end	
	end
end)
--Intermission
local function roundTimer()
	while wait() do
		for i = intermissionLength, 0, -1 do
			InRound.Value = false
			wait(1)
			Status.Value = "Intermission In ".. i .. ""
		end
		for i = roundLength, 0, -1 do
			InRound.Value = true
			wait(1)
			Status.Value = "Game Ends In ".. i ..""
		end
	end
end

spawn(roundTimer)

If you know tips or ways to fix/make the script better, please let me know.

In the intermission script it says when the intermission is done, the InRound.Value becomes false

When i look at the teleport script, i can see the InRound.Value == true to tp the players

So right now you are tp’ing the players to the lobby when the intermission is over

When the roundlength timer starts thats when the players gets tp’ed so if i change it the players gets tp’ed the the lobby when the round starts. So it turns true when the round starts and then they get tp’ed.

1 Like

When the intermission is over does the other for loop start?
Try to insert some prints(“?”) to see what doesn’t work and what works

This is the error im getting

ServerScriptService.MainScript:54: attempt to call a table value]

Stack Begin

Script ‘ServerScriptService.MainScript’, Line 54]

Stack End

1 Like

Remove the brackets: for i, v in pairs(survivor) do --here

And here: for i, v in pairs(killer) do – here

well nothing happens now, when the round starts :confused:

and if i run the game with two players i get this error

ServerScriptService.MainScript:11: invalid argument #2 to ‘random’ (interval is empty)
Stack Begin

Script ‘ServerScriptService.MainScript’, Line 11 - function PickPlayers
Script ‘ServerScriptService.MainScript’, Line 35

Stack End

1 Like

So i edited the script and here is the working version:

wait(3) -- Added a wait time

--Killer/Survivor Picker

local killer = nil
local survivors = {}

function PickPlayers()
	
	survivors = {}
	
	local plrs = game.Players:GetChildren()
	local KillerID = math.random(1, #plrs)

	for i, v in pairs(plrs) do
		if i == KillerID then
			killer = v.Name
			break
		end
	end
		
	for i, v in pairs(plrs) do
		if i == KillerID then
			
		else
			table.insert(survivors, v.Name)
		end
	end
	
	print("Killer: ".. killer)
	for i, v in pairs(survivors) do
		print("Survivor: ".. v)
		
	end
end

while wait(5) do
	PickPlayers()
	print("Picked")
	break
end

--Intermission, Roundtimer and Teleport

local roundLength = 100
local intermissionLength = 10
local InRound = game.ReplicatedStorage.InRound
local Status = game.ReplicatedStorage.Status

local LobbySpawn = game.Workspace.Lobby.Spawns.LobbySpawn
local GameAreaSpawn = game.Workspace.Hallway.Spawns.MapSpawn
local KillerAreaSpawn = game.Workspace.WaitingRoom.KillerWaitingSpawn

--Teleport

InRound.Changed:Connect(function()
	if InRound.Value == true then
		for i, v in pairs(survivors) do
			game.Workspace[v].HumanoidRootPart.CFrame = GameAreaSpawn.CFrame -- Finding the player in the workspace
		end
		print(killer) -- Removed the else
		game.Workspace[killer].HumanoidRootPart.CFrame = KillerAreaSpawn.CFrame -- Finding the player in the workspace
	end
end)


InRound.Changed:Connect(function()
	if InRound.Value == false then
		for i, v in pairs(game.Players:GetChildren()) do
			game.Workspace[v].HumanoidRootPart.CFrame = LobbySpawn.CFrame
		end	
	end
end)
--Intermission
local function roundTimer()
	while wait() do
		for i = intermissionLength, 0, -1 do
			InRound.Value = false
			wait(1)
			Status.Value = "Intermission In ".. i .. ""
		end
		for i = roundLength, 0, -1 do
			InRound.Value = true
			print("InRound = true")
			wait(1)
			Status.Value = "Game Ends In ".. i ..""
		end
	end
end

spawn(roundTimer)

Thank you so much, but there is still a problem, it dosnt work in multiplayer, any ideas of how?

i get this error

ServerScriptService.MainScript:12: invalid argument #2 to ‘random’ (interval is empty)

Stack Begin

Script ‘ServerScriptService.MainScript’, Line 12 - function PickPlayers

Script ‘ServerScriptService.MainScript’, Line 37

Stack End

I found out why, there must be enough players than the minimum math.random before the script starts.

Thank you for the help :smiley: