Is it possible to check if a function in a module script has finished?

So, I have a script that calls for a function inside a module script. Say for example, it’s the most basic. The function has a wait(3), then prints(“E”). Is there a way I can check if that function has finished running?

function DoLOTSofStuff()
wait(3)
end

DoLOTSofStuff()
–next line doesnt run till 3 seconds after since the function yeilds for 3 seconds

alternativly u could use bindable events and whatnot

Would this not be affected by lag? The function could complete before the original wait has finished. So, if an animation plays and ends in the function, the wait could still be running and nothing would happen because of server lag.

Before you run the function, define a variable, and then change that variable to true when the function is finished. You can check that variable when you need to know if the function has completed.

local functionHasFinished = false
-- function here
functionHasFinished = true

-- elsewhere
if functionHasFinished then
--foo
end
1 Like

It would work, I was just hoping that there was an easier solution. It is two different scripts, so if there isn’t a way to transfer info from the module script first to the script, I’d have to change a bool value and then check if it was changed using the script.

This would NEVER happen. I think the format of my post may have confused you. Here’s the formated version:

function foo()
     wait(3) --the thread that the function was called in will yeild for 3 seconds
end

local start = tick()
foo()
print(tick()-start, "seconds") --Should be ~3 seconds

In that case, you can use either a BoolValue or a BindableEvent to signal between scripts. Both are objects that you place in the game, so both scripts can reference and interact with them.
For a BoolValue, script 1:

local functionFinishedBool = game.blahblahblah --path to where you put the boolvalue

function foo()
    wait(3)
    functionFinishedBool.Value = true
end

script 2:

local functionFinishedBool = game.blahblahblah --path to where you put the boolvalue

if functionFinishedBool.Value == true then
    --whatever
end

For a BindableEvent, script 1:

local functionBindableEvent = game.blahblahblah --path to where you put the bindableevent

function foo()
    wait(3)
    functionBindableEvent:Fire(true)
end

script 2:

local functionBindableEvent = game.blahblahblah --path to where you put the bindableevent
local functionFinished = false

functionBindableEvent.Event:Connect(function(isFinished)
    functionFinished = true
end)

if functionFinished then
    --whatever
end
1 Like

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