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

Is there a way to wait for an event, only for a certain amount of time?

Asked by 3 years ago

I know you can do

event:Wait()

but I don't want to do this because the player who I'm waiting for may troll afk. Is there a way to do this except only for a certain amount of time?

1 answer

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Unlike Instance:WaitForChild(), RBXScriptSignals do not have a timeout argument. If you wish to enable this behaviour, you'll have to build a function to handle this:

local function BindRBXSignalTimeout(RBXScriptSignal, Timeout)
--// Activate the Event and prepare to catch the parameters.
    ---------------
    local Response, Connection;
    Connection = RBXScriptSignal:Connect(function(...)
        Response = {...}
        Connection:Disconnect()
    end)
    ---------------
--// Check whether we received any parameters or if the timeout is reached.
    ---------------
    local TimeoutMarker = (tick() + (Timeout or 30))
    repeat wait() until (Response or tick() >= TimeoutMarker)
    ---------------
--// Cut the Event or return the caught parameters.
    ---------------
    if (Response) then
        return unpack(Response)
    else
        Connection:Disconnect()
    end
end

local Part = workspace.Part


local PartCollided = BindRBXSignalTimeout(Part.Touched, 5)

if (PartCollided) then
    print(PartCollided.Name.." made contact with "..Part.Name)
else
    warn(Part.Name.."'s '.Touched' Event timed out.")
end
0
Thx so much Dr_Smartypants123 86 — 3y
Ad

Answer this question