coreography/src/shared/Modules/Utilities/waitWithTimeout.luau
2026-03-02 22:23:43 +01:00

35 lines
669 B
Plaintext

--!strict
return function(event: RBXScriptSignal, timeoutInSeconds: number): (boolean, ...any)
local thread = coroutine.running()
local connection: RBXScriptConnection?
local function onEvent(...)
if not connection then
return
end
connection:Disconnect()
connection = nil
if coroutine.status(thread) == "suspended" then
task.spawn(thread, false, ...)
end
end
connection = event:Once(onEvent)
task.delay(timeoutInSeconds, function()
if not connection then
return
end
connection:Disconnect()
connection = nil
if coroutine.status(thread) == "suspended" then
task.spawn(thread, true)
end
end)
return coroutine.yield()
end