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)()
local connection = RunService.Heartbeat:Connect(LoopStep)
-- later:
-- replace
stopped = true
-- with
if connection then
connection:Disconnect()
connection = nil
end
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)