What does Event:Wait(1) do?

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.

I heard that it acts like a timeout or something?

It does not do anything at all.

1 Like

On WaitForChild, yes. The documentation for this says that it does nothing though.

Sorry if this is off topic but, what happens if WaitForChild() has reached a timeout?

It simply returns nil. I’m pretty sure.

Wait() function does not have any arguments so that do nothing.

3 Likes

It will return nothing, and your code can error if you don’t expect that.
so to fix it, you do this

hrp = character:WaitForchild("HumanoidRootPart", 3)
if hrp then
---stuff
else
   warn("No humanoid root part!")
end
1 Like

Well, it looks like I’m gonna have a hard time on how to handle NPCs that gets stuck in certain situations. Do you have an idea on how to handle this?

When this is fired:

obj:WaitForChild("Object")

The script will wait for Object forever, or until it is found.

When this is fired:

obj:WaitForChild("Object",5)

The script will wait for Object for 5 seconds. If it cannot find the object in 5 seconds, it will return as nil.

2 Likes

In this case, let’s say I wanna use timeout for waypoints for NPC. How can I like implement a timeout for it to jump in case the NPC is stuck?

1 Like

I have idea

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

Uh, how does it set fires back to false again?

That will loop forever until fired variable is true.

  if tick() > timeout + starttime then
     istimeout = true
     break
  end

It should be like this.

1 Like

You can make function for this

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
3 Likes

Sorry i was wrong
i edited post

That’s just wrong. Where’d you get that idea from?

4 Likes

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)