I am scripting a game where a timer counts down for intermission, and when the timer runs out, a map is chosen and everyone is taken to it. After 2 minutes, they get sent back. While this code works in studio, it wont even run online; the print statements at the beginning are not shown in the console. The script is located in the Workspace and it is not a LocalScript.

As I'm not sure where the issue lies, I'll just post all of the script here. (Sorry, it's kind of lengthy!)

print('hello?')
while wait(.01) do
	print('Map Switcher Run!')
	local Players = game.Players:GetChildren()
	local Spawn = game.Workspace.Spawn
	local Countdown = game.Workspace.CountdownVal
	local Msg = Instance.new('Message')
	
	function Timer(time)
		print('Timer Run!')
		for i = time, 0, -1 do
			wait(1)
			for j = 1, #Players do
				Players[j].PlayerGui.ScreenGui.Countdown.Text = tostring(i)
			end
		end
	end
	
	Timer(60)
	local RandMap = math.random(1,2)
	print('map: ' .. RandMap)
	if RandMap == 1 then
		Msg.Parent = game.Workspace
		Msg.Text = 'Map Chosen: Chartin'
		wait(3)
		Msg.Parent = nil
		game.Lighting.Maps.Chartin:Clone().Parent = game.Workspace
		local Chartin = game.Workspace.Chartin
		for i = 1, #Players do
			local RandSpawn = math.random(1,6)
			print(Players[i].Name .. ' spawn: ' .. RandSpawn)
			if RandSpawn == 1 then
				Players[i].Character:MoveTo(Chartin.SpawnLocation1.Position)					
			elseif RandSpawn == 2 then
				Players[i].Character:MoveTo(Chartin.SpawnLocation2.Position)
			elseif RandSpawn == 3 then
				Players[i].Character:MoveTo(Chartin.SpawnLocation3.Position)
			elseif RandSpawn == 4 then
				Players[i].Character:MoveTo(Chartin.SpawnLocation4.Position)
			elseif RandSpawn == 5 then
				Players[i].Character:MoveTo(Chartin.SpawnLocation5.Position)
			elseif RandSpawn == 6 then
				Players[i].Character:MoveTo(Chartin.SpawnLocation6.Position)
			end
		end
		Timer(120)
		Chartin:Remove()
	elseif RandMap == 2 then
		Msg.Parent = game.Workspace
		Msg.Text = 'Map Chosen: Rustic'
		wait(3)
		Msg.Parent = nil
		game.Lighting.Maps.Rustic:Clone().Parent = game.Workspace
		local Rustic = game.Workspace.Rustic
		for i = 1, #Players do
			local RandSpawn = math.random(1,8)
			print(Players[i].Name .. ' spawn: ' .. RandSpawn)
			if RandSpawn == 1 then
				Players[i].Character:MoveTo(Rustic.SpawnLocation1.Position)					
			elseif RandSpawn == 2 then
				Players[i].Character:MoveTo(Rustic.SpawnLocation2.Position)
			elseif RandSpawn == 3 then
				Players[i].Character:MoveTo(Rustic.SpawnLocation3.Position)
			elseif RandSpawn == 4 then
				Players[i].Character:MoveTo(Rustic.SpawnLocation4.Position)
			elseif RandSpawn == 5 then
				Players[i].Character:MoveTo(Rustic.SpawnLocation5.Position)
			elseif RandSpawn == 6 then
				Players[i].Character:MoveTo(Rustic.SpawnLocation6.Position)
			elseif RandSpawn == 7 then
				Players[i].Character:MoveTo(Rustic.SpawnLocation7.Position)
			elseif RandSpawn == 8 then
				Players[i].Character:MoveTo(Rustic.SpawnLocation8.Position)
			end
		end
		Timer(120)
		Rustic:Remove()
	end
	for i = 1, #Players do
		local RandSpawn = math.random(1,5)
		if RandSpawn == 1 then
			Players[i].Character:MoveTo(Spawn.SpawnLocation1.Position)					
		elseif RandSpawn == 2 then
			Players[i].Character:MoveTo(Spawn.SpawnLocation2.Position)
		elseif RandSpawn == 3 then
			Players[i].Character:MoveTo(Spawn.SpawnLocation3.Position)
		elseif RandSpawn == 4 then
			Players[i].Character:MoveTo(Spawn.SpawnLocation4.Position)
		end
	end
	Msg.Parent = game.Workspace
	Msg.Text = 'Game Over!'
	wait(3)
	Msg.Parent = nil
end

2 comments

0

I'm going to assume this script runs immediately. Add a brief delay before the script starts, or don't start the script at all until there are players in the server.

0

Moreover, this script is likely to break if a player leaves in the middle of it.