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.
I use the coroutine.close() function to stop the water from rising/the coroutine from running. But it gives me the 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)