Hey! Thought I’d share a small module I made that helps out with handling many events at once, called WaitFor.
WaitFor.all(…)
After requiring WaitFor, you can use this function to create a signal that only fires once all signals you pass into it are fired. The returned signal will only fire once.
Example;
local GameLoadedAndPlayerAdded = WaitFor.all(game.Loaded, Players.PlayerAdded)
GameLoadedAndPlayerAdded:Connect(print) --> will fire after both game.Loaded and Players.PlayerAdded have fired
WaitFor.any(…)
You can use this function to create a signal that only fires once one of the signals you pass into it are fired. The returned signal will only fire once, and will pass as arguments: the signal you passed in which fired, and any arguments which that signal passed.
Example;
local Part1Changed = workspace.Part1.Changed
local Part2Changed = workspace.Part2.Changed
local Part3Changed = workspace.Part3.Changed
local AnyPartChanged = WaitFor.any(Part1Changed, Part2Changed, Part3Changed)
AnyPartChanged:Connect(function(event, ...) --> will fire when one part changes
if event == Part1Changed then
print("Part 1 was changed!")
elseif event == Part2Changed then
print("Part 2 was changed!")
else
print("Part 3 was changed!")
end
end)
Try it for yourself
I’ve included a small sample place to show you how the module can be used: WaitFor.rbxl (21.1 KB)
Click the coloured parts to send a signal - the two larger parts light up when their signal is fired.
Get the module
You can find the module on the Roblox library here.
Alternatively, you can download the source code directly: WaitFor.lua (2.2 KB)
Hopefully you guys might find this a bit useful, let me know if there are any issues