Making script wait until event was fired

I was working on a script when I needed to make it delay until an object was touched. I saw this post, and I implemented this line of code

repeat task.wait() until Object.Touched

After several modifications, I couldn’t get it to work. My goal here is to make the entire script wait until this event from this part get fired and make the script proceed.

1 Like

Events on objects are RBXScriptSignal. Instead of using :Connect() to bind a function to all future triggers of this event, you can use :Wait() to yield the current thread until only the next event.

1 Like

Have the main script call a function in which the part is created and a listener to it being touched is added, then return something random like “done” and call that function getting its return value, that will cause the code to yield until a value is returned from the function and it will only return a value thus stopping the part when the part is touched

1 Like
repeat task.wait() until Object.Touched:Wait()

Something like that?

No need for the repeat loop. Simply:

print("This will print before the Touched event")
Object.Touched:Wait()
print("This will print after the Touched event")

(This assumes Object is a reference to some instance with a Touched event, like a Part)

2 Likes

you would need to test the object each loop…

Would be nice to see the script here …
That’s a lot better … East98

1 Like

Thanks a lot for your help! It really helped!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.