Issue with time-stop command

The Loop function won’t stop when “stopped” is true

coroutine.wrap(function()
    --while wait() do
    if not stopped then
        Loop = RunService.Heartbeat:Connect(LoopStep)
        print("not stopped")
    else
        print("?")
        Loop:Disconnect(LoopStep)
        print("stopped")
    end
    --end
end)()

It’s because it’s not disconnecting the heartbeat function, or maybe it needs to run again to disconnect it.

1 Like

can you give me an example on how to disconnect it?

local connection = RunService.Heartbeat:Connect(LoopStep)
-- later:
-- replace
stopped = true
-- with
if connection then
    connection:Disconnect()
    connection = nil
end
1 Like

thank you that worked, but do you know how do I start the time loop again?

You can make functions like this:

local loop
local function Stop()
    if loop then
        loop:Disconnect()
        loop = nil
    end
end

local function Start()
    Stop()
    loop = RunService.Heartbeat:Connect(LoopStep)
end

Start()

Or to be neater, you can do this:

RunService.Heartbeat:Connect(function(dt)
    if not stopped then LoopStep(dt) end
end)
1 Like

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