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?
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