Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a function wait to execute until another function has finished before it?

What method(s) do I use to make a function wait until another function has finished?

2 answers

✓ Accepted answer

Scripts are pretty linear. There's not often that you need to do something like this because a function will automatically wait for that function to finish until it continues. There is one way to do this, though, if needed.

local FuncDone = false

function f1()
	FuncDone = false -- Reset it in case of multiple calls
	Spawn(function() -- Start a new thread
		print("A")
		wait(3)
		print("B")
		FuncDone = true
	end)
end

function f2()
	f1()
	repeat wait(1/20) until FuncDone
	print("C")
end

f2()

You could have a function that returns true or false, and then have a repeat statement.

So for example:

function f1()
	if 1==1 then
		return true
	else
		return false
	end
end

function f2()
	f1()
	repeat wait() until f1
end

This discussion is preserved as it was. New answers, comments, and votes are closed.