Touch event not registering

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
1 Like

You’re exhausting the script execution time, besides you should use :GetTouchingParts() to check if it is touching something.

1 Like

It’s caused because it is running the function for every frame that the ball is being touched.

1 Like

Ye forgot to mention its in a heartbeat connection

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)

1 Like

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.

I use run service for moving the ball and checking a bunch of stuff (forgot that event listeners are ran every frame)

You can just use ball:GetTouchingParts()

I did, it just completely ignores a part and registers other parts

Hm, that’s weird, I guess just get the Touched connection out of the Heartbeat event and you should be good.

I did everything you said it still doesnt work, perhaps its an engine bug? Replication has no delay and note that the ball is constantly moving

It’s most likely not an engine bug, but it’s weird nonetheless.

It just fires when it touches a character

I looped thru ball:GetTouchingParts() and an empty touched event connection (The ball isnt supposed to collide)

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