.Touched Event so laggy when it is inside while loop?

Hello,

so i’m trying to make something like this
https://i.gyazo.com/d9aa892f3371556e6ec272bd4065ba0f.mp4
(Without .Touched)

the issue here is when i put .touched event inside the loop it becomes something like this, even though there is debounce in it
https://i.gyazo.com/e0daaf5d55a3cfca41263b3496a993a2.mp4
(With .Touched)

I’m trying to fix it right now but no luck

pls, send me your code…

or at least, where the touch event is, because it must be outside the loop
so you will define touched event, and after it, will run the loop


well its something like this and yes there is wait(0.1) at the end of the loop

Is there a reason you’re putting an event inside the loop? The event will fire even if you only set the callback once. That’s the point of an event. People have complained about that event being inaccurate though, so you could always try to use this inside of RenderStepped then disconnect RenderStepped when you want to disable it.

Well the only reason i did that is to apply the .touched event for every spike mesh created

this is worst way, how you can do it, because you are creating the event every loop run, you must create empty local spike and than spawn this function

while Spike==nil
end
Spike.Touched:Connect(...................)

Looks like you’re doing it on the client. I’ve never seen what happens when you set the event callback in a while loop before really quickly but I could guess it probably isn’t the best thing ever. Try using BindToRenderStep and something like this module by EgoMoose that allows for rotated region3s to detect when something is in an area. As long as you don’t stress it out too much then you shouldn’t lag too much. If you’re messing with timings then make sure to use the delta time so it isn’t based on frame rate.

i’ll try those u both suggested thanks

You’ll want to check out this thread:

In simple, parts with complex geometries can cause the lag you mentioned when working with Touched events. Consider using magnitude checks or FindPartsInRegion3 as alternative solutions.

If possible, I recommend changing the part’s CollisionFidelity from Default to Box too.

2 Likes