I have a function that takes an event and timeout duration as arguments and returns nil if the event is not fired within the timeout duration, as shown below.

local rbxUtility = LoadLibrary("RbxUtility")
function waitUntilTimeout(event, duration)
	local signal = rbxUtility.CreateSignal()
	local connection = nil
	connection = event:Connect(function(...)
		connection:Disconnect()
		signal:Fire(...)
	end)
	delay(duration, function()
		if connection ~= nil then
			connection:Disconnect()
			connection = nil
			signal:Fire(nil)
		end
	end)
	return signal:Wait()
end

Is it possible to define my own custom events? For example, I need an event to fire when a humanoid's "Shield" (this is an IntValue) decreases, but not when it increases. The code below would not work for me because I need it to be all under one event so that my waitUntilTimeout function can take it as an argument.

local previousShield = shield.Value
shield.Changed:Connect(function()
	currentShield = shield.Value
	if previousShield > currentShield then
		print("Shield decreased")
	end
	previousShield = shield.Value
end)

2 comments

0

LoadLibrary is deprecated.

0

Yep, I wasn't sure how else to do it and I couldn't find anything recent on it.