How do I wait for a function to return something?

Hi. Simple question, how do I wait for a function to return something and then run code when it does?

I just have a simple timer function that counts down from whatever the second argument is (like 30 seconds). When it reaches 0, I want it to return that it reached 0 so that the script can do a certain thing.

local function HurryUpNoob(running, seconds)
	while wait(1) do
		if running == true then
			if seconds < 1 then
				running = false
				return "Time exceeded"
			end
			
			seconds = seconds - 1
			Request.Countdown.Text = seconds
			
		else break end
	end
end

EventFolder.KeypadClick.OnClientEvent:Connect(function()
	local hh = true
	local grogg = 30
	HurryUpNoob(hh, grogg)

    -- i think it would go somewhere here
	
	Frame.Visible = true
end)

You can coroutine a function called (for example) TimerReachedZero after a while loop. The code from loop will run on a new thread.

It looks like you should have that intended behavior already, can you show me exactly what the issue is?

use spawn or corotines, cant believe i said await HAHA

Mate, this is Lua, not C# or JavaScript/TypeScript. Async and await doesn’t exist here. All functions are syncronised by default and will yield the code until the code inside function finishes running.

Lmao sorry about that, but then you would use spawn() or coro, or look at this epic article

coming from ts its cool cuz promises

Your code will execute synchronously so you simply put whatever you want in the place of your comment in the code and it will run after the function returns.

2 Likes