Is there a way to 'pause' scripts and resume them from another script?

I want to pause a script while it’s running from another script and then resume it later on in the script that paused it. I just don’t know if there’s a way to pause scripts and resume scripts from another one.

2 Likes

Maybe you could provide more details, like specifically what you the script’s purpose is? Because some there might be different methods depending on what the script does

1 Like

What I have are two local scripts that take input from the user and either make them dash or do an attack. What I’m trying to do is basically temporarily stop the other script after one has taken the correct input e.g. When the dash script is used then I want the attack script to not do anything until the dash script is over. I also forgot to mention that I can’t use script.Enabled because i have a debounce in the scripts which gets reset every time the script gets restarted

1 Like

You could use an instance Boolean value, compress both scripts into one, or you could try using asynchronous methods to call and resume.

local process = coroutine.create(function()
    --blah blah
    coroutine.yield()
end)
1 Like

Sorry if this is a stupid question but can you tell me what coroutines are and what they are used for?

Coroutines are Lua’s way of programming asynchronously. That’s a fancy way of saying doing multiple things at once. In Roblox Luau, you can also use task.spawn, but for the purpose of this I’ll stick to coroutines.

coroutine.create

This will create a new coroutine out of the provided function. Coroutines are basically the things that will run at the same time.

--create a new coroutine
local co = coroutine.create(function(...)
    --... means any number of parameters
    print(...)
    task.wait(1)
    print("done")
end)

coroutine.resume(co) --if we called co normally the script would wait until it had completed
--a coroutine means it will run seperately and not halt the script

print("hi")

coroutine.wrap will create a new coroutine from the provided function and you can just call it to resume the function.

local function foo()
end

local bar = coroutine.wrap(foo)
bar() --calls it as if it was a coroutine

coroutine.yield

This is for use with coroutine.resume, explained below. It will exit the given coroutine until it is resumed again.

local co = coroutine.create(function()
    print("one")
    coroutine.yield()
    print("two")
    coroutine.yield()
    print("three")
    coroutine.yield()
end)

--we would need to resume co 3 times for it to finish
coroutine.resume(co) --this would only output "one"

coroutine.resume

Resumes the given coroutine from the point it left off, or from the start if it hasn’t started yet.

local co = coroutine.create(function()
    print("hi")
    coroutine.yield()
    print("bye")
end)

coroutine.resume(co)

This is just a brief overview of some of asynchronous programming in Lua. Check out Roblox’s documentation for more information.

Is it possible to yield using bindable events or do they have to be set yields within the coroutine?

At this point it might be better to combine the scripts. But yes, you could yield with events.

local co = coroutine.create(function()
    print("one")
    coroutine.yield()
    print("two")
end)

coroutine.resume(co)
BindableEvent.Event:Wait()
coroutine.resume(co)

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