Add timeout parameter for Event:Wait()

If you click on this part of a post you can see which post is being replied to:

blob.png

I was replying to your code sample just above where you suggest to return the parameters when the event fired within the timeout interval, or otherwise nil if it did not fire within the interval. In my previous post I explain why it won’t work out well.

Dumb but relevant example (since I can’t think of another event without parameters right now), what if I wanted to check if a user focuses the window in the next 3 seconds with your suggestion?

local result = game.UserInputService.WindowFocused:Wait(3)
if result then
   -- ...
end

This actually won’t work at all, because WindowFocused doesn’t have any event parameters. So this suggestion, where you return nil instead of event parameters (which may also be nil) when the timeout has expired, does not work.

Instead think something like this:


local status = pcall(
   function()
      return game.UserInputService.WindowFocused:Wait(3)
   end
)

if status then
   -- fired within 3 seconds
else
   -- did not fire
end

And something similar for an event with parameters where you also use the rest of the output of pcall.

5 Likes