The title may be a bit unclear, but you know how wait() functions go with events?
RBXScriptSignal:wait() -- Pauses the script until event is fired
Lets say I want to only wait up to 180 seconds until that event is fired. Also I want to connect the same function to the wait time, so even after 180 seconds pass, I can fire that function too (Even if the event doesn't fire).
Do you have any suggestions?
My idea is to just use a
while wait() do end
type loop, that break
s either when time is out or when the event happens.
local happened = false con = Event:connect(function() happened = true end) -- record that the event happened local stopTime = tick() + 180 -- 180 seconds from now... while tick() < stopTime do wait() if happened then break end end con:disconnect() -- clean up the old connection
Another, weirder, but briefer option would be to make a BindableEvent and set up both things to fire it, then destroy the connection/bindable event:
local e = Instance.new("BindableEvent") local c = Event:connect(e:Fire()) -- when the event happens delay(180, function() e:Fire() end) -- in 180 seconds e.Event:wait() -- the sooner of the two c:disconnect() -- cleanup the connection