How to pause a script during wait(x)

heyo
i want to make a system where a player does an action that temporarily pauses everything around them, and each player has a moveset that is structured something like this:

local function action()
    debounce = true
    changeSpeed()
    doAnimation()
    wait(0.5) ~~this is important because the pause action could happen during this moment
    changeSpeed()
    debounce = false
end

it’s a somewhat crude representation but I would like to know if there’s a way you can pause a script during its wait(x) function and resume the script after the paused event. If not I would also like to know some alternatives to wait(x)

You can try to listen for a specific event through Event:Wait(), which yields the code until fired.

sorry, i wasn’t completely specific in my post
i don’t need the code below the wait() to fire when the pause event happens, i only want to find out if the script can be paused during wait() and resume its wait countdown after

You can’t really yield anything in between, not even disabling the script works. Try a different approach as I have previously mentioned. It is simply technically not possible.

One workaround might be using wait differently, such as below:

--instead of just doing
wait(0.5)
--try this?
local pause = false -- somewhere else, can be set to true or false

local function action()
     debounce = true
     changeSpeed()
     doAnimation()
     local timer = 0.5
     while timer > 0 do
          local deltaTime = wait()
          if not pause then
               timer -= deltaTime
          end
     end
     changeSpeed()
     debounce = false
end

The while loop will only update the timer when pause is false, otherwise it’ll wait and not do anything

4 Likes

this seems like a great workaround! ill try this out and ill get back to you