Hi, I have a problem with touch event, basically Im trying to check if ball is touching a wall but its very delayed, theres a huuuge lag spike and “touched” is printed 5k times in a single frame that lasts a few seconds.
ball.Touched:Connect(function(hit)
if hit.Name == "Rig/hitbox" then
print("touched")
end
My recommendation is to add a debounce, once the ball is touched make a variable such as a boolean and make that boolean true. So then once the ball is hit. It will only run once it’s hit and won’t run again until whatever you do after.
local Touched = false
ball.Touched:Connect(function(hit)
if (hit.Name) == 'rig/hitbox' and not (Touched) then
print('Touched')
Touched = true
end
end)
That’s a huge issue, that’d mean that you’re creating a new connection every frame and that would explain the insane lag. Think of it like this, the Touched event fires 60 times per second and the Hearbeat event fires every 60 seconds or so too. That’d mean that it registers around 3600 touches every second. So that explains the lag spike.
Edit: My math ain’t mathing, it’s actually registering around ms^2 + ms / 2 touched events. (The amount of print calls per second is limited to 5000 I believe)
Adding onto that, you don’t need run service in order to check if something was hit. Simply doing ball.touched is enough to check whether the ball got hit or not. I don’t see a good reason as to why using run service is a good idea for that.