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

How can I make a loop (Or any code) wait for an event?

Asked by 9 years ago

I want to make a for loop wait for an event to fire before executing the code in it again. In the code block below is basically what I want.

1for  i,v in pairs(t) do
2    thing:DoTheThing(v)
3    (Wait for event to fire)
4end

(Note: This isn't the full code, this is only the loop)

In case you're curious or have any alternate solution, this is for pathfinding to a certain location using the pathfinding service.

How would I go about this sort of thing?

1 answer

Log in to vote
1
Answered by
MrNicNac 855 Moderation Voter
9 years ago

Events have a :wait() method which will wait until it fires, then return any parameters.

http://wiki.roblox.com/index.php?title=Event

1local Part = workspace.Part
2 
3local Hit = Part.Touched:wait()
4print(Hit)

As you've mentioned, you should use it in a loop. The :wait() method does not automatically make a looping occurrence like :connect() does with a function.

Ad

Answer this question