When I was learning some scripting, I noticed the scripter wrote this line.
Event:Wait(1)
Now, if theres no numbers in the brackets of Wait, then I understand it is that the event will pause the script until it was fired. However, if there’s numbers inside it, what does it do?
I believe this is a very simple question to answer, and I can’t find the answer on devforum sadly.
I have a hunch that it does nothing, that anything in the brackets is just ignored. It will still wait for the event to be fired, and not a moment more or less.
local timeout = 10
local somesignal = something.Event
local istimeout
local fired
somesignal:Connect(function()
fired = true
end)
local starttime = tick()
while wait(.1) do
if fired then break end
if tick() > timeout + starttime then
istimeout = true
break
end
end
if istimeout then
warn("Timeout!")
end
function waitfirewithtimeout(event, timeout)
local istimeout
local fired
event:Connect(function()
fired = true
end)
local starttime = tick()
while wait(.1) do
if fired then break end
if tick() > timeout + starttime then
istimeout = true
break
end
end
if istimeout then
warn("Timeout!")
end
end
I made something like that for a Zombie AI I wrote a while ago, remember it may look ugly because I optimized it, the function is for one individual zombie only (each zombie had its own script), however you can modify it.
local CustomMoveTimeout
do
local Dummy = Instance.new("BoolValue")
local Changed = Dummy:GetPropertyChangedSignal("Value")
CustomMoveTimeout = function(Event,Time)
Dummy.Value = false
local Returns
local Connection;Connection = Event:Connect(function(Reached)
Connection:Disconnect()
Connection = nil
Returns = Reached
Dummy.Value = true
end)
do
coroutine.wrap(function()
wait(Time)
if (Returns) then return end
if (not Connection) then return end
Connection:Disconnect()
Connection = nil
Dummy.Value = true
end)()
end
Changed:Wait()
return Returns
end
end
You use it like this:
local HasReachedGoal = CustomMoveTimeout(Humanoid.MoveToFinished,Timeout)