My Code refuses to run functions!

(Made an error, see next comment for corrected code)

You could use coroutine.wrap() in this case:

coroutine.resume(GenerateRoom, "Prologue")

Remotes.ProceedLevel.OnServerEvent:Connect(function(Player:Player)
	coroutine.wrap(function() -- A one time coroutine every time the event is triggered
		if CurrentRoom.LevelComplete.Value == true then
			local NextRoom = CurrentRoom:FindFirstChild("NextRoom").Value
			coroutine.resume(GenerateRoom, NextRoom)
		end
	end)

end)

Sorry I made an error, try this:

coroutine.resume(GenerateRoom, "Prologue")

Remotes.ProceedLevel.OnServerEvent:Connect(function(Player:Player)
	local Newcoroutine = coroutine.create(function() -- A one time coroutine every time the event is triggered
		if CurrentRoom.LevelComplete.Value == true then
			local NextRoom = CurrentRoom:FindFirstChild("NextRoom").Value
			coroutine.resume(GenerateRoom, NextRoom)
		end
	end)
	coroutine.resume(Newcoroutine)
end)
1 Like

It’s pretty odd, the next-room doesn’t seem to be generating.

try creating a new coroutine first then resuming it

Here is an updated script:

coroutine.resume(GenerateRoom, "Prologue")

Remotes.ProceedLevel.OnServerEvent:Connect(function(Player:Player)
	local Newcoroutine = coroutine.create(function() -- A one time coroutine every time the event is triggered
		if CurrentRoom.LevelComplete.Value == true then
			local NextRoom = CurrentRoom:FindFirstChild("NextRoom").Value
			
			local nxtRm = coroutine.create(GenerateRoom, NextRoom)
			coroutine.resume(nxtRm)
		end
	end)
	coroutine.resume(Newcoroutine)
end)

I think it may be that the loop is never ending, which is stopping the other functions from running. Check that out.

Also, alot of task.spawns. I rarely use task.spawn, the only time I ever did was for a loading icon. Use it sparingly.

If you really need to use those task.spawns, don’t write them INSIDE the function like you are currently doing. Just use task.spawn(function)

1 Like

It’s pretty weird, the line where it prints ‘Generating Next-Room’ doesn’t seem to run.
Here’s the code that I have currently.

function PreloadZones(MusicParts,CameraParts,ForceParts,NPCs,ExitZone)
	task.wait(1)
	SetForceRE:FireAllClients(ExitZone, "Exit")	
	task.wait(1)

	for _, MPart in pairs(MusicParts) do
		if MPart and MPart:IsA("BasePart") and MPart:FindFirstChild("MConfig") then
			SetMusicRE:FireAllClients(MPart)
			print("Server located a new Music-Zone.")
		end
	end

	task.wait(1)

	for _, CamPart in pairs(CameraParts) do
		if CamPart and CamPart:IsA("BasePart") and CamPart:FindFirstChild("ZConfig") then
			ChangeCamRE:FireClient(MainPlayer, CamPart)
			print("Server located a new Camera-Zone.")
		end
	end

	task.wait(1)

	for _, FPart in pairs(ForceParts) do
		if FPart and 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(MainPlayer, FPart, nil)
			print("Server located a new Force-Zone.")
		end
	end

	task.wait(1)

	for _, Char in pairs(NPCs) do
		if Char:IsA("Model") then		
			SetNPC:FireClient(MainPlayer, Char, nil)
			print("Server located a new NPC.")
		end
	end
	
	local TimeSpent = CurrentRoom.TimeSpent
	local LevelComplete =CurrentRoom.LevelComplete
	
	while true do
		if LevelComplete.Value == true then break end
		TimeSpent.Value += 1
		task.wait(1)
	end
end

local GenerateRoom = coroutine.create(function(RoomName:string)
	print("Generating Level: " .. RoomName)

	MainCharacter.PrimaryPart.Anchored = true

	for _, Junk in pairs(CurrentLevel:GetChildren()) do
		if Junk.Name ~= RoomName then
			Junk:Destroy()
		end
	end

	if CurrentLevel:FindFirstChild(RoomName) then
		CurrentRoom = CurrentLevel:FindFirstChild(RoomName)
	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"

	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

	MainCharacter.PrimaryPart.Anchored = false

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

coroutine.resume(GenerateRoom, "Prologue")

Remotes.ProceedLevel.OnServerEvent:Connect(function(Player:Player)
	print("Proceeding onto the Next-Level!")
	
	local Newcoroutine = coroutine.create(function()
		if CurrentRoom.LevelComplete.Value == true then
			local NextRoom = CurrentRoom:FindFirstChild("NextRoom").Value
			local nxtRm = coroutine.create(GenerateRoom, NextRoom)
			coroutine.resume(nxtRm)
		end
	end)
	coroutine.resume(Newcoroutine)
end)

Honestly, I would try using the code without task.spawn or coroutines and see if it runs correctly, it should still run correctly without them.

Or you could try turning the script you have into a module, wich could work, but may require extra work.

Heya, I managed to find a solution but all this worked! Thank you!

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