'Cannot resume dead coroutine' although I didn't call resume

I am making an obby stage where the player has to complete the obby and click the stop button (see image) before the water rises and they will die.

Obby Stage

I use the coroutine.close() function to stop the water from rising/the coroutine from running. But it gives me the Error although I haven’t called coroutine.resume() afterwards.

Here is my script:

local WaterModel						=			Info[1]
local ProxPrompt						=			Info[2]
		
local function RiseWater()
	while wait() do
			WaterModel:SetPrimaryPartCFrame(CFrame.new(WaterModel.PrimaryPart.Position.X,WaterModel.PrimaryPart.Position.Y + 0.02, WaterModel.PrimaryPart.Position.Z))
		end
	end
		
	local RiseWaterCoroutine				=			coroutine.create(RiseWater)
	coroutine.resume(RiseWaterCoroutine)
		
ProxPrompt.Triggered:Connect(function()
	coroutine.close(RiseWaterCoroutine)
end)

Is it maybe because I first have to yield the coroutine? I have no clue.

Your coroutine is created within RiseWater, are you sure you want that to happen?

Besides, I think you don’t necessarily need coroutines for this:

local WaterModel = Info[1]
local ProxPrompt =	Info[2]

local WaterRising = true

ProxPrompt.Triggered:Connect(function()
	WaterRising = false
end)

task.spawn(function()
	while WaterRising do
        -- For that matter, SetPrimaryPartCFrame() also isn't good.
        -- Use SetPivot instead, it's exactly the same, but works better.
		WaterModel:SetPivot(WaterModel:GetPivot():ToWorldSpace(CFrame.new(0, 0.2, 0)))
        -- If you're wondering what ToWorldSpace() is, it offsets the CFrame by another one.
	    task.wait() -- Also, while wait() do isn't a great idea.
        -- Put it inside the loop instead, like I've done here.
    end
end)
1 Like

Thanks man! I’m now using task.spawn() for all the coroutines in my script cuz it’s more efficient.

Although I think it is called :PivotTo and not :SetPivot and I want to increase the Y by 0.02

Yes, my bad, it is PivotTo and not SetPivot, I always mess them up because it’s GetPivot, so in my head it should be SetPivot too.

Yeah that would really make sense