My Code refuses to run functions!

Hello, I’ve been having a major issue where I am trying to make a level loading system. In these levels are a bunch of zones and they are all not working. Only the NPCs loading function seems to be working. I have tried using a multitude of task.spawn functions to try and fix my code and make each function run and well… work. Here’s the code:

(You will see a lot of task.spawn spam as I have panicked 10x more than usual)

Necessary Code:

local function SetCamZones(CameraParts)
	task.spawn(function()
		for _, CamPart in pairs(CameraParts) do
			if CamPart:IsA("BasePart") and CamPart:FindFirstChild("ZConfig") then
				ChangeCamRE:FireClient(NewPlayer, CamPart)
			end
		end
	end)
end

local function SetNPCs(NPCs)
	task.spawn(function()
		for _, Char in pairs(NPCs) do
			if Char:IsA("Model") then			
				SetNPC:FireClient(NewPlayer, Char, nil)
			end
		end
	end)
end

local function SetForceZones(ForceParts)	
	task.spawn(function()
		for _, FPart in pairs(ForceParts) do
			if FPart:IsA("BasePart") and FPart:FindFirstChild("Waypoints") then
				if not FPart:FindFirstChild("Completed") then
					local NewBool = Instance.new("BoolValue", FPart)
					NewBool.Name = "Completed"
				end

				SetForceRE:FireClient(NewPlayer, FPart, nil)
			end
		end
	end)
end

local function SetMusicZones(MusicParts)
	task.spawn(function()
		for _, MPart in pairs(MusicParts) do
			if MPart:IsA("BasePart") and MPart:FindFirstChild("MConfig") then
				SetMusicRE:FireClient(NewPlayer, MPart)
			end
		end
	end)
end

function GenerateRoom(RoomName:string)
	task.spawn(function()
		if Levels:FindFirstChild(RoomName) then
			local NewRoom = Levels:FindFirstChild(RoomName):Clone()
			NewRoom.Parent = CurrentLevel

			local TimeSpent = Instance.new("IntValue", NewRoom);TimeSpent.Name = "TimeSpent"
			local LevelComplete = Instance.new("BoolValue", NewRoom);LevelComplete.Name = "LevelComplete"

			local CameraParts = NewRoom:FindFirstChild("CameraParts")
			local MusicParts = NewRoom:FindFirstChild("MusicParts")
			local ForceParts = NewRoom:FindFirstChild("ForceZones")
			local Objectives = NewRoom:FindFirstChild("Objectives")
			local NPCs = NewRoom:FindFirstChild("NPCs")

			local EntranceLocation = nil

			if NewRoom:FindFirstChild("EntranceLift") then
				local EntLift = NewRoom:FindFirstChild("EntranceLift")
				local EntLiftDirection = EntLift:FindFirstChild("Direction")

				local MainLift = EntLift:FindFirstChild("MainLift")			
				local Waypoint = MainLift:FindFirstChild("Waypoint")
				Character:PivotTo(Waypoint.FixPos.CFrame)
			elseif NewRoom:FindFirstChildOfClass("SpawnLocation") then
				Character:PivotTo(NewRoom:FindFirstChildOfClass("SpawnLocation").CFrame * CFrame.new(0,5,0))
			end

			DefaultService.SpawnBox:Destroy()

			local ExitLift = NewRoom:FindFirstChild("ExitLift")
			local ExitZone = ExitLift:FindFirstChild("ExitZone")

			SetForceRE:FireClient(NewPlayer, ExitZone, "Exit")

			print("Setting up Force-Zones in Room: " .. NewRoom.Name)
			task.spawn(function()SetForceZones(ForceParts:GetChildren())end)

			print("Setting up Camera-Zones in Room: " .. NewRoom.Name)
			task.spawn(function()SetCamZones(CameraParts:GetChildren())end)

			print("Setting up Music-Zones in Room: " .. NewRoom.Name)
			task.spawn(function()SetMusicZones(MusicParts:GetChildren())end)

			print("Setting up NPCs in Room: " .. NewRoom.Name)
			task.spawn(function()SetNPCs(NPCs:GetChildren())end)

			if NewRoom:FindFirstChild("Spy") then 
				local SpyModule = require(NewRoom.Spy)
				SpyModule.Track(NewPlayer)
			end

			Remotes.CompleteLevel.OnServerEvent:Connect(function(Player)
				NewRoom.LevelComplete.Value = true
				task.wait(3)
				Remotes.CompleteLevel:FireClient(Player, NewRoom)

				local ZoneController = require(Modules.Zone.ZoneController)
				local AllZones = ZoneController.getZones()

				for _, Zone in pairs(AllZones) do
					Zone:destroy()
				end
			end)

			while true do
				if LevelComplete.Value == true then break end
				TimeSpent.Value += 1
				task.wait(1)
			end
		end
	end)
end

GenerateRoom("Prologue")
7 Likes

Is there a reason you start a new thread inside every function? Rather calling the function in a new thread instead? Also you don’t need to make an anonymous function everytime you need to call it in a thread you can just use task.spawn(func, ...)

It also seems like you’re creating new threads twice in a row, that’s bad performance.

3 Likes

I didn’t really consider of running it all in one thread, also: Can I have an example of the whole ‘non-anonymous’ function stuff?

1 Like

I already showed you an example.

local function _unNouveauFunction(_lesArguments: any)
	print(_lesArguments)
end

task.spawn(_unNouveauFunction, "Salut !")
3 Likes

The Server seems to be completely clean now but I believe the issue may be on the client. It’s probably as messy as the Server and I have probably… no idea on how to sort it all out!

EDIT: The only thing working seems to be the Dialog, but none of the zones are functioning…
image

1 Like

Here’s the Client Code, it’s uhhh. something. I can’t really find the issue in here too.

function SetForceZone(SpecPart:BasePart, IsExit)
	task.spawn(function()
		if SpecPart and SpecPart:FindFirstChild("Waypoints") and IsExit == nil then
			local Completed = SpecPart:FindFirstChild("Completed")
			local Waypoints = SpecPart:FindFirstChild("Waypoints")
			local NewReg = ZoneModule.new(SpecPart)

			NewReg:trackItem(Player.Character)
			NewReg.accuracy = ZoneModule.enum.Accuracy.High

			local MainMap = SpecPart.Parent.Parent
			local LevelComplete = MainMap:FindFirstChild("LevelComplete")

			task.spawn(function()
				NewReg.playerEntered:Connect(function(player:Player)
					if not Debounce then
						Debounce = true

						local PlayerModule = require(player:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule"))
						local Controls = PlayerModule:GetControls()
						local Character = player.Character
						local Humanoid = Character:FindFirstChildOfClass("Humanoid")

						Controls:Disable()

						for I=1, #Waypoints:GetChildren() do
							if Waypoints[I] then
								Humanoid:MoveTo(Waypoints[I].Position)

								task.spawn(function()
									if Waypoints[I]:FindFirstChild("Function") then
										if Waypoints[I].Function.Value == "ForceCam" then CameraModule.Restore() end
										if FuncAssistor[Waypoints[I].Function.Value] then
											FuncAssistor[Waypoints[I].Function.Value](Waypoints[I].Function.Target.Value)
										end
									end
								end)

								Humanoid.MoveToFinished:Wait()
							end
						end

						repeat task.wait() until Completed.Value == true
						task.wait(0.55)
						SpecPart.CanTouch = false
						Controls:Enable(true)
						Debounce = false
					end
				end)
			end)

		elseif SpecPart and IsExit == "Exit" then
			local ExitZone = SpecPart
			local MainLift = ExitZone.Parent:FindFirstChild("MainLift")
			local Waypoint = MainLift:FindFirstChild("Waypoint")
			local Direction = SpecPart.Parent:FindFirstChild("Direction")

			local NewReg = ZoneModule.new(SpecPart)
			NewReg:trackItem(Player.Character)
			NewReg.accuracy = ZoneModule.enum.Accuracy.High

			local LiftOffset = 215
			if Direction.Value == "Down" then LiftOffset = -LiftOffset end

			task.spawn(function()
				NewReg.playerEntered:Connect(function(EntPlayer:Player)
					if not Debounce then
						Debounce = true

						local PlayerModule = require(EntPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule"))
						local Controls = PlayerModule:GetControls()
						local Character = EntPlayer.Character
						local Humanoid = Character:FindFirstChildOfClass("Humanoid")

						Controls:Disable()
						Humanoid:MoveTo(Waypoint.Position)
						Remotes.EditState:FireServer("Immobile", true)
						task.spawn(function() FuncAssistor.ForceCam(MainLift.ExitCamera) end)
						local LiftTrack = FuncAssistor.TweenModel(MainLift, MainLift.PrimaryPart.CFrame * CFrame.new(0,LiftOffset,0), LiftInfo)
						Humanoid.MoveToFinished:Wait()
						Character.PrimaryPart.CFrame = Waypoint.FixPos.CFrame

						local WeldCons = Instance.new("WeldConstraint", Waypoint.FixPos)
						WeldCons.Part0 = Waypoint.FixPos
						WeldCons.Part1 = Character.PrimaryPart

						task.wait(1.5)
						MainLift.PrimaryPart.Lever:Play()
						task.wait(0.75)
						MainLift.PrimaryPart.Start:Play()
						LiftTrack:Play()
						task.wait(1)
						MainLift.PrimaryPart.Loop:Play()
					end
				end)
			end)
		end
	end)
end

function SetMusicZone(SpecPart:BasePart)
	task.spawn(function()
		if SpecPart and SpecPart:FindFirstChild("MConfig") then
			local ZoneConf = SpecPart:FindFirstChild("MConfig")
			local NewReg = ZoneModule.new(SpecPart)
			NewReg:trackItem(Player.Character)

			NewReg.accuracy = ZoneModule.enum.Accuracy.Medium

			task.spawn(function()
				NewReg.playerEntered:Connect(function(player)
					if not Debounce then
						Debounce = true

						for _, Sound in pairs(ZoneConf:GetChildren()) do
							if Sound:IsA("Sound") then
								if not Sound:GetAttribute("OriginalVolume") then
									Sound:SetAttribute("OriginalVolume", Sound.Volume)
								end

								Sound:Play()
								Sound.Volume = 0
								Sound.TimePosition = 0

								TweenService:Create(Sound, QuickInfo, {Volume = Sound:GetAttribute("OriginalVolume")}):Play()
							end
						end

						task.wait(0.55)
						Debounce = false
					end
				end)

				NewReg.playerExited:Connect(function(player)
					if not Debounce then
						Debounce = true
						for _, Sound in pairs(ZoneConf:GetChildren()) do
							if Sound:IsA("Sound") then
								TweenService:Create(Sound, QuickerInfo, {Volume = 0}):Play()
							end
						end

						task.wait(0.275)
						for _, Sound in pairs(ZoneConf:GetChildren()) do
							if Sound:IsA("Sound") then
								Sound:Stop()
							end
						end
						task.wait(0.275)
						Debounce = false
					end
				end)
			end)
		end
	end)
end

function ChangeCam(SpecPart:BasePart)
	task.spawn(function()
		if SpecPart and SpecPart:FindFirstChild("ZConfig") then
			local ZoneConf = SpecPart:FindFirstChild("ZConfig")
			local ZoneType = ZoneConf:FindFirstChild("ZoneType")
			local NewReg = ZoneModule.new(SpecPart)
			NewReg:trackItem(Player.Character)

			NewReg.accuracy = ZoneModule.enum.Accuracy.High

			task.spawn(function()
				NewReg.playerEntered:Connect(function(player)
					if not Debounce then
						Debounce = true

						if ZoneConf:FindFirstChild("Settings") then
							for _, Setting in pairs(ZoneConf:FindFirstChild("Settings"):GetChildren()) do
								if CamConfigMod[Setting.Name] then
									CamConfigMod[Setting.Name] = Setting.Value
								end
							end
						end

						if ZoneType.Value == "Isometric" then
							CameraModule.EnableIsometricCamera()
							CameraModule.DisableShiftLockCamera()
							CameraModule.DisableSideScrollingCamera()
						elseif ZoneType.Value == "SideScrolling" then
							CameraModule.EnableSideScrollingCamera()
							CameraModule.DisableIsometricCamera()
							CameraModule.DisableShiftLockCamera()
						elseif ZoneType.Value == "OverTheShoulder" then
							CameraModule.EnableShiftLockCamera()
							CameraModule.DisableIsometricCamera()
							CameraModule.DisableSideScrollingCamera()
							CameraModule.FaceCharacterToMouse()
						end
						task.wait(0.025)
						Debounce = false
					end
				end)

				NewReg.playerExited:Connect(function(player)
					if not Debounce then
						Debounce = true
						CameraModule.Restore()
						task.wait(0.025)
						Debounce = false
					end
				end)
			end)
		end
	end)
end

Where do you call the client functions?

They are called via Remote Events

Did you try removing the IsExit from the if statement and seeing if it works then?

Hm. Let me try that. I’ll let you know if it has any changes.

Nope, Here’s my new code, which still. Doesn’t decide to even run.

local function CreateNewZone(Part:BasePart)
	local NewZone = ZoneModule.new(Part)
	NewZone:trackItem(Player.Character)
	NewZone.accuracy = ZoneModule.enum.Accuracy.High

	return NewZone
end

function SetForceZone(SpecPart:BasePart, IsExit)
	task.spawn(function()
		if SpecPart then
			local Completed = SpecPart:FindFirstChild("Completed")
			local NewZone = CreateNewZone(SpecPart)

			if SpecPart:FindFirstChild("Waypoints") then
				local Waypoints = SpecPart:FindFirstChild("Waypoints")

				NewZone.playerEntered:Connect(function(NewPlayer:Player)
					if not ForceZoneDebounce then
						ForceZoneDebounce = true

						Controls:Disable()

						for I=1, #Waypoints:GetChildren() do
							if Waypoints[I] then
								Humanoid:MoveTo(Waypoints[I].Position)

								task.spawn(function()
									if Waypoints[I]:FindFirstChild("Function") then
										if Waypoints[I].Function.Value == "ForceCam" then CameraModule.Restore() end
										if FuncAssistor[Waypoints[I].Function.Value] then
											FuncAssistor[Waypoints[I].Function.Value](Waypoints[I].Function.Target.Value)
										end
									end
								end)

								Humanoid.MoveToFinished:Wait()
							end
						end

						repeat task.wait() until Completed.Value == true
						task.wait(0.55)
						SpecPart.CanTouch = false
						Controls:Enable(true)
						ForceZoneDebounce = false
					end
				end)
			else
				if IsExit == "Exit" then
					local ExitZone = SpecPart
					local MainLift = ExitZone.Parent:FindFirstChild("MainLift")
					local Waypoint = MainLift:FindFirstChild("Waypoint")
					local Direction = SpecPart.Parent:FindFirstChild("Direction")

					local LiftOffset = 215
					if Direction.Value == "Down" then LiftOffset = -LiftOffset end

					NewZone.playerEntered:Connect(function(EntPlayer:Player)
						if not Debounce then
							Debounce = true

							local PlayerModule = require(EntPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule"))
							local Controls = PlayerModule:GetControls()
							local Character = EntPlayer.Character
							local Humanoid = Character:FindFirstChildOfClass("Humanoid")

							Controls:Disable()
							Humanoid:MoveTo(Waypoint.Position)
							Remotes.EditState:FireServer("Immobile", true)
							task.spawn(function() FuncAssistor.ForceCam(MainLift.ExitCamera) end)
							local LiftTrack = FuncAssistor.TweenModel(MainLift, MainLift.PrimaryPart.CFrame * CFrame.new(0,LiftOffset,0), LiftInfo)
							Humanoid.MoveToFinished:Wait()
							Character.PrimaryPart.CFrame = Waypoint.FixPos.CFrame

							local WeldCons = Instance.new("WeldConstraint", Waypoint.FixPos)
							WeldCons.Part0 = Waypoint.FixPos
							WeldCons.Part1 = Character.PrimaryPart

							task.wait(1.5)
							MainLift.PrimaryPart.Lever:Play()
							task.wait(0.75)
							MainLift.PrimaryPart.Start:Play()
							LiftTrack:Play()
							task.wait(1)
							MainLift.PrimaryPart.Loop:Play()
						end
					end)
				end
			end
		end
	end)
end

function SetMusicZone(SpecPart:BasePart)
	task.spawn(function()
		if SpecPart and SpecPart:FindFirstChild("MConfig") then
			local ZoneConf = SpecPart:FindFirstChild("MConfig")
			local NewZone = CreateNewZone(SpecPart)

			NewZone.playerEntered:Connect(function(EnteringPlayer)
				if not MusicZoneDebounce then
					MusicZoneDebounce = true

					for _, Sound in pairs(ZoneConf:GetChildren()) do
						if Sound:IsA("Sound") then
							if not Sound:GetAttribute("OriginalVolume") then
								Sound:SetAttribute("OriginalVolume", Sound.Volume)
							end

							Sound:Play()
							Sound.Volume = 0
							Sound.TimePosition = 0

							TweenService:Create(Sound, QuickInfo, {Volume = Sound:GetAttribute("OriginalVolume")}):Play()
						end
					end

					task.wait(0.55)
					MusicZoneDebounce = false
				end
			end)

			NewZone.playerExited:Connect(function(LeavingPlayer)
				if not MusicZoneDebounce then
					MusicZoneDebounce = true

					for _, Sound in pairs(ZoneConf:GetChildren()) do
						if Sound:IsA("Sound") then
							TweenService:Create(Sound, QuickerInfo, {Volume = 0}):Play()
						end
					end

					task.wait(0.275)

					for _, Sound in pairs(ZoneConf:GetChildren()) do
						if Sound:IsA("Sound") then
							Sound:Stop()
						end
					end

					task.wait(0.275)
					MusicZoneDebounce = false
				end
			end)
		end
	end)
end

function ChangeCam(SpecPart:BasePart)
	task.spawn(function()
		if SpecPart and SpecPart:FindFirstChild("ZConfig") then
			local ZoneConf = SpecPart:FindFirstChild("ZConfig")
			local ZoneType = ZoneConf:FindFirstChild("ZoneType")
			local NewReg = ZoneModule.new(SpecPart)
			NewReg:trackItem(Player.Character)

			NewReg.accuracy = ZoneModule.enum.Accuracy.High

			task.spawn(function()
				NewReg.playerEntered:Connect(function(player)
					if not Debounce then
						Debounce = true

						if ZoneConf:FindFirstChild("Settings") then
							for _, Setting in pairs(ZoneConf:FindFirstChild("Settings"):GetChildren()) do
								if CamConfigMod[Setting.Name] then
									CamConfigMod[Setting.Name] = Setting.Value
								end
							end
						end

						if ZoneType.Value == "Isometric" then
							CameraModule.EnableIsometricCamera()
							CameraModule.DisableShiftLockCamera()
							CameraModule.DisableSideScrollingCamera()
						elseif ZoneType.Value == "SideScrolling" then
							CameraModule.EnableSideScrollingCamera()
							CameraModule.DisableIsometricCamera()
							CameraModule.DisableShiftLockCamera()
						elseif ZoneType.Value == "OverTheShoulder" then
							CameraModule.EnableShiftLockCamera()
							CameraModule.DisableIsometricCamera()
							CameraModule.DisableSideScrollingCamera()
							CameraModule.FaceCharacterToMouse()
						end
						task.wait(0.025)
						Debounce = false
					end
				end)

				NewReg.playerExited:Connect(function(player)
					if not Debounce then
						Debounce = true
						CameraModule.Restore()
						task.wait(0.025)
						Debounce = false
					end
				end)
			end)
		end
	end)
end

Remotes.SetMusicZone.OnClientEvent:Connect(SetMusicZone)
ChangeCamRE.OnClientEvent:Connect(ChangeCam)
Remotes.SetForceZone.OnClientEvent:Connect(SetForceZone)

Im starting to give up… Here’s my Server Code except I see no problem with it. It’s all weird as the Dialog is the only thing running, AKA. The NPC scripts.

function PrepareZones(ForceParts, MusicParts, CamParts, NPCs)
	for _, CamPart in pairs(CamParts) do
		if CamPart:IsA("BasePart") and CamPart:FindFirstChild("ZConfig") then
			ChangeCamRE:FireClient(NewPlayer, CamPart)
		end
	end

	for _, FPart in pairs(ForceParts) do
		if FPart:IsA("BasePart") and FPart:FindFirstChild("Waypoints") then
			if not FPart:FindFirstChild("Completed") then
				local NewBool = Instance.new("BoolValue", FPart)
				NewBool.Name = "Completed"
			end

			SetForceRE:FireClient(NewPlayer, FPart, nil)
		end
	end

	for _, MPart in pairs(MusicParts) do
		if MPart:IsA("BasePart") and MPart:FindFirstChild("MConfig") then
			SetMusicRE:FireClient(NewPlayer, MPart)
		end
	end

	for _, Char in pairs(NPCs) do
		if Char:IsA("Model") then			
			SetNPC:FireClient(NewPlayer, Char, nil)
		end
	end
end

function GenerateRoom(RoomName:string)
	task.spawn(function()
		if Levels:FindFirstChild(RoomName) then
			local NewRoom = Levels:FindFirstChild(RoomName):Clone()
			NewRoom.Parent = CurrentLevel

			local TimeSpent = Instance.new("IntValue", NewRoom);TimeSpent.Name = "TimeSpent"
			local LevelComplete = Instance.new("BoolValue", NewRoom);LevelComplete.Name = "LevelComplete"

			local CameraParts = NewRoom:FindFirstChild("CameraParts"):GetChildren()
			local MusicParts = NewRoom:FindFirstChild("MusicParts"):GetChildren()
			local ForceParts = NewRoom:FindFirstChild("ForceZones"):GetChildren()
			local NPCs = NewRoom:FindFirstChild("NPCs"):GetChildren()
			local Objectives = NewRoom:FindFirstChild("Objectives")

			local EntranceLocation = nil

			if NewRoom:FindFirstChild("EntranceLift") then
				local EntLift = NewRoom:FindFirstChild("EntranceLift")
				local EntLiftDirection = EntLift:FindFirstChild("Direction")

				local MainLift = EntLift:FindFirstChild("MainLift")			
				local Waypoint = MainLift:FindFirstChild("Waypoint")
				Character:PivotTo(Waypoint.FixPos.CFrame)
			elseif NewRoom:FindFirstChildOfClass("SpawnLocation") then
				Character:PivotTo(NewRoom:FindFirstChildOfClass("SpawnLocation").CFrame * CFrame.new(0,5,0))
				NewRoom:FindFirstChildOfClass("SpawnLocation").Enabled = true
			end

			local ExitLift = NewRoom:FindFirstChild("ExitLift")
			local ExitZone = ExitLift:FindFirstChild("ExitZone")

			SetForceRE:FireClient(NewPlayer, ExitZone, "Exit")

			print("Preparing Zones for Room: " .. NewRoom.Name)
			task.spawn(PrepareZones, ForceParts, MusicParts, CameraParts, NPCs)
			
			if NewRoom:FindFirstChild("Spy") then 
				local SpyModule = require(NewRoom.Spy)
				SpyModule.Track(NewPlayer)
			end

			Remotes.CompleteLevel.OnServerEvent:Connect(function(Player)
				NewRoom.LevelComplete.Value = true
				task.wait(3)
				Remotes.CompleteLevel:FireClient(Player, NewRoom)

				local ZoneController = require(Modules.Zone.ZoneController)
				local AllZones = ZoneController.getZones()

				for _, Zone in pairs(AllZones) do
					Zone:destroy()
				end
			end)

			while true do
				if LevelComplete.Value == true then break end
				TimeSpent.Value += 1
				task.wait(1)
			end
		end
	end)
end

GenerateRoom("Prologue")
1 Like

Yep, still not working. I have no ideas what to do… Any argument sent through the remote-event keeps thinking that they are nil and wont resume.

1 Like

Is this one just impossible then?

1 Like

you probably put Player as argument on OnClientEvent…
you don’t have to since LocalPlayer is the player

1 Like

I am not completely sure what to do about your problem, but you could try coroutine.wrap() here’s an example:

task.spawn(function()
	SetNPCs(NPCs:GetChildren())
end)

coroutine:

coroutine.wrap(SetNPCs,NPCs:GetChildren()) --You don't use brackets to call the functions

OK! So I SOMEHOW managed to get it working. I have no idea what I did but it’s starting to work. I have a question however. How do I make like… A new coroutine restart and that?

local GenerateRoom = coroutine.create(function(RoomName:string)
	if CurrentLevel:FindFirstChild(RoomName) then
		CurrentRoom = CurrentLevel:FindFirstChild(RoomName)
		
		for _, Junk in pairs(CurrentLevel:GetChildren()) do
			if Junk.Name ~= RoomName then
				Junk:Destroy()
			end
		end
		
	elseif Levels:FindFirstChild(RoomName) then
		CurrentRoom = Levels:FindFirstChild(RoomName):Clone()
		CurrentRoom.Parent = CurrentLevel
	end

	local TimeSpent = Instance.new("IntValue", CurrentRoom);TimeSpent.Name = "TimeSpent"
	local LevelComplete = Instance.new("BoolValue", CurrentRoom);LevelComplete.Name = "LevelComplete"
	
	task.spawn(function()
		while true do
			if LevelComplete.Value == true then break end
			TimeSpent.Value += 1
			task.wait(1)
		end
	end)
	
	local CameraParts = CurrentRoom:FindFirstChild("CameraParts"):GetChildren()
	local MusicParts = CurrentRoom:FindFirstChild("MusicParts"):GetChildren()
	local ForceParts = CurrentRoom:FindFirstChild("ForceZones"):GetChildren()
	local NPCs = CurrentRoom:FindFirstChild("NPCs"):GetChildren()
	local Objectives = CurrentRoom:FindFirstChild("Objectives")

	local ExitLift = CurrentRoom:FindFirstChild("ExitLift")
	local ExitZone = ExitLift:FindFirstChild("ExitZone")

	print("Preparing Zones for Room: " .. CurrentRoom.Name)	

	task.spawn(PreloadZones,MusicParts,CameraParts,ForceParts,NPCs,ExitZone)
	task.wait(0.85)

	if CurrentRoom:FindFirstChild("EntranceLift") then
		local EntLift = CurrentRoom:FindFirstChild("EntranceLift")
		local EntLiftDirection = EntLift:FindFirstChild("Direction")
		local MainLift = EntLift:FindFirstChild("MainLift")			
		local Waypoint = MainLift:FindFirstChild("Waypoint")

		local LiftOffset = 215
		local TargetFrame = MainLift.PrimaryPart.CFrame
		if EntLiftDirection.Value == "Down" then LiftOffset = -LiftOffset end

		MainLift:PivotTo(MainLift.PrimaryPart.CFrame * CFrame.new(0,LiftOffset,0))
		MainCharacter:PivotTo(Waypoint.FixPos.CFrame)

		local NewTrack = FuncAssistor.TweenModel(MainLift, TargetFrame, LiftInfo)
		MainLift.PrimaryPart.Start:Play()
		MainLift.PrimaryPart.Loop:Play()
		NewTrack:Play()
		NewTrack.Completed:Wait()
		MainLift.PrimaryPart.Start:Stop()
		MainLift.PrimaryPart.Loop:Stop()
		MainLift.PrimaryPart.Lever:Play()
	elseif CurrentRoom:FindFirstChildOfClass("SpawnLocation") then
		MainCharacter:PivotTo(CurrentRoom:FindFirstChildOfClass("SpawnLocation").CFrame * CFrame.new(0,5,0))
		CurrentRoom:FindFirstChildOfClass("SpawnLocation").Enabled = true
	end

	if CurrentRoom:FindFirstChild("Spy") then 
		local SpyModule = require(CurrentRoom.Spy)
		SpyModule.Track(MainPlayer)
	end
end)

coroutine.resume(GenerateRoom, "Prologue")

Remotes.ProceedLevel.OnServerEvent:Connect(function(Player:Player)
	if CurrentRoom.LevelComplete.Value == true then
		local NextRoom = CurrentRoom:FindFirstChild("NextRoom").Value
		coroutine.resume(GenerateRoom, NextRoom)
	end
end)

Im not very knowledgable about coroutines but I think this is how it works:

local func = coroutine.create(functionToRun)  --create coroutine

coroutine.resume(func) -- run coroutine

coroutine.close(func) -- stop coroutine

Stopping then resuming a coroutine seems to restart it

After stopping a coroutine you can still resume using coroutine.resume()

So how would I use it in this case?

coroutine.resume(GenerateRoom, "Prologue")

Remotes.ProceedLevel.OnServerEvent:Connect(function(Player:Player)
	if CurrentRoom.LevelComplete.Value == true then
		local NextRoom = CurrentRoom:FindFirstChild("NextRoom").Value
		coroutine.resume(GenerateRoom, NextRoom)
	end
end)