A problem I need help with

So basically, I need a way for a wait trigger in the function not to stop the other code below it.
Example code:

(edit) OOPS I MADE A MISTAKE sorry guys, I said function but I meant a “for” loop. for example:

ExampleRemote.OnServerEvent:Connect(function()
for i, v in pairs(workspace:GetPartsInPart(ExamplePart)) do
       print("ExampleText")
       wait(1)
       print("ExampleText2")
       end
       --I want this code to run as the loop above is running
       print("ExampleText3")
end)

Please help I’ve been trying to fix this for like an hour lol

1 Like

I think you just need to fix the typo on this line:

Haha, I was going to reply with task.delay too, until I realized OP’s code should be doing exactly what they want it too, only if that typo wasn’t there.

First of all, it’s never a good practice to nest functions inside of functions as it creates memory leaks.

But as this is an example, here is a code snippet:

local old_connection = nil
ExampleRemote.OnServerEvent:Connect(function()
       if old_connection then
              old_connection:Disconnect()
       end)
       old_connection = ExamplePart.Touched:Connect(function()
              print("ExampleText")
              task.delay(1,function()
                     print("ExampleText2")
              end)
       end)
       --I want this code to run as the function above is running
       print("ExampleText3")
end)
1 Like

Using task.delay is “a way for a wait trigger in the function not to stop the other code below it” so that’s probably the OP’s solution.

Remember to update old_connection!

old_connection = ExamplePart.Touched:Connect(function()
1 Like

If you want the example3 to run while still waiting 1 second for example 2, you can use coroutine.wrap.

For example:

 print("ExampleText")
 
coroutine.wrap(function()
 	wait(1)
	print("ExampleText2")
 end)()
 
 print("ExampleText3")

Yes, I know that. Why are you mentioning that?

Thanks for correcting me!

1 Like

fine lol

I know this wont fix the problem entirely, but for my issue it was a touch thing and putting the for i, v loop inside a script in the part worked. I don’t know why i didn’t think of this before lol. Anyway thank you all for the help!

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