Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a way to use "Break" on RunService?

Asked by 3 years ago

Is there any method to break RunService? It seems like

game:GetService("RunService").Heartbeat:Connect(function()
print("Hello")
break
end)

Doesn't work...

2 answers

Log in to vote
3
Answered by 3 years ago

You could disconnect the connection after setting the connection to a variable. It would look something like this,

local Connection
Connection = RunService.Heartbeat:Connect(function()
    Connection:Disconnect()
end)
Ad
Log in to vote
3
Answered by
gskw 1046 Moderation Voter
3 years ago
Edited 3 years ago

@iiNemo has a great answer if you want an event-connection-based solution. Sometimes you might want it in a loop, so you can use break like this:

while true do
    -- do your stuff
    if condition then
        break   
    end
    RunService.Heartbeat:Wait()
end

If you want to wait for Heartbeat only once, you can simply use RunService.Heartbeat:Wait() just like a wait() call.

Answer this question