How can I pause/cancel a script?

I wanted to know how can I cancel/pause a script mid-way.

1 Like

Well, it would depend on what you’re doing in the script. Can you elaborate

You can do that by doing

path.to.script.Disabled = true

replace path.to.script with the script path.

Create a yield or loop that will end when desired. If you want to cancel a script, you can use a return. If you want to also disconnect events and break loops, you can disable the script like @nonamehd24 said and then return immediately after (because disabling will not end execution, it will only stop loops and events).

3 Likes

Ever played any 3d Pokémon games? In battle, your camera starts moving as idle until you interact. After interaction, the camera immediately goes to the initial spot, and stops moving.

I want to replicate exactly that, but I just don’t know how.

Call

script:Destroy()

if you want to cancel a script.
or

script.Disabled = true

on your script.

1 Like

There are a few ways, it entirely depends on how you lay the script out tho.
I would use promises, which is more advanced but it would work.

https://eryn.io/roblox-lua-promise/

I’m pretty sure the code in the script still executes

Thank you, I will try that. I’ll mark as solution if it works.

You know, there are things called Breakpoints that you select which line the code should stop on

Sorry for replying 10h after, I was sleeping.

I don’t want to stop on a specific line, I want to stop a block immediately after event being fired.

Would you mind showing us your code with a comment on the line you want to stop on?

This still isn’t enough context. Can you provide an example as to what exactly you want? There are many ways to stop a function, thread, or script mid-way and it depends a lot on the context of what you’re doing at the time and how exactly you want it to stop.

Well I guess you could just destroy the script at the end of the function line

Interrupting wait-while loops can be done like this, to restart the while loop would require putting it into a function and using another event.

local running = true
bindableEvent.Event:Connect(function()
   running = false
end)

while task.wait() do
    if not running then
        break
    end

    -- constantly running code
end

-- after exit clean up code
1 Like

Doing this from the script doesn’t actually stop script execution, it just runs in nil. For a behavior like that you’d need to disable the script from another script.

I needed that also. Thank you. :white_check_mark:

1 Like