.Touched is laggy even when debouncing

local HTTPs = game:GetService('HttpService')
local T = tick()
local Radars = {}

Radars.AddRadar = function(Part)
	Part.Transparency = 1
	Part.CanCollide = false
	Part.Touched:Connect(function(HitPart)
if (tick() - T) < 4 then
			return
		end
			T = tick()
		if HitPart:IsDescendantOf(workspace.Map) then
			return
		end
		
	
		 		local Player = game.Players:GetPlayerFromCharacter(HitPart.Parent)
		if (Player) then
			if (Player.Team ~= game.Teams.Civilian) then
				return
			end
			if (Player.Character) then
				if (Player.Character:FindFirstChild('Humanoid') or false) then
					if (Player.Character.Humanoid.Health > 0) then
						local Velocity = Player.Character.HumanoidRootPart.Velocity.Magnitude/2
						if (Velocity > tonumber(Part.Name) or 100) then
							print(Player:GetFullName(),' speeding with a velocity of: '..Velocity)
						end 
					end
				end
			end
		end
	end)
end

return Radars

That’s my code.
It’s a radar, that detects if player’s speeding.
Works amazing!
Just… one thing.
It lags a lot when going with vehicles.
How can I fix this?
Should I use Physics, and set collide groups?
Edit: tried that, still same results

1 Like

This part might be pretty expensive and not completely necessary. Say there was hundreds of thousands of instances inside Workspace → Map than that function would need to check against several things.

if HitPart:IsDescendantOf(workspace.Map) then
    return
end

might be contributing to lag.

1 Like

Oh, I though, it would actually reduce, so basically, doesnt search on game.Players:getPlayerFromCharacter()
I’ll test 1 sec.

1 Like

It reduced, still it’s quite laggy

Dang. How many radar parts do you create?

It’s just 2. and it’s getting .Touched event only in one, that’s not trouble I think

1 Like

.Touched will fire every time the part is touched, and with high detail items made of thousands of parts, this can fire 1000s of times in a second. You have a few choices:
• Reduce the size of the part that the touched event is connected to
• Reduce the amount of parts a vehicle has
• FindPartsInRegion3
• Changing to a distance based function
I would change the size of the part to make it only like a speed bump on the road that the wheels touch.

2 Likes

Yes, I would do the speed bump thingy, but sadly it needs to be touched by character, when it’s sit on a car

If it is the case of too many touch events fired because of several parts another good solution would be creating the collision groups. with like only the humanoid root part and the radars.

I did that, collision groups fire .Touched

Why doesn’t the Debounce work?
It should run once each 4 seconds.

Indeed, it’s only running once.


But than why does it lags?

1 Like

Could you name the car the player’s name or place the wheels in a consistent place (under a “wheel” model under the car model or something like that) that you can get the car’s seat and get the occupant of the seat and get the player that way?
Example:

Part.Touched:Connect(function(Hitpart)
    if Hitpart.Parent.Name == "Wheels" and Hitpart.Parent.Parent.Seat.Occupant then
        -- do the check for speeding, but get the Occupant of the seat, which is a humanoid.
    end
end)
2 Likes

I’ll try out and let you know.