Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

1 comment

0

Thx so much

This discussion is preserved as it was. New answers, comments, and votes are closed.