Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How could I mimic Roblox's RBXScriptSignals?

Asked by 5 years ago

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)
0
LoadLibrary is deprecated. User#19524 175 — 5y
0
Yep, I wasn't sure how else to do it and I couldn't find anything recent on it. Perthrosama 24 — 5y

Answer this question