Touched event constantly firing. What to do?

Alright, so say for example I have 200 parts. Each of them is programmed to deal damage to a player. However, this function runs multiple times and I believe it’s causing the game to lag. Is there any way to deal with this? I’ve tried adding a debounce, but it really isn’t making a change.
Code:

local Bone = script.Parent
local Damage = script.Damage

Bone.Touched:Connect(function(hit)
	if hit.Parent ~= nil and hit.Parent:FindFirstChild("Humanoid") then
		local Humanoid = hit.Parent.Humanoid
		if Humanoid ~= nil then
			Humanoid.Health = Humanoid.Health - Damage.Value
		end
	end
end)

Specifics: The parts are close together, the parts are moved using a script, the parts are unions, the parts have CanCollide and CanQuery off, the parts are anchored.

1 Like
local debounce: boolean

Bone.Touched:Connect(function(hit)
    if debounce then return end
    debounce = true
	if hit.Parent ~= nil and hit.Parent:FindFirstChild("Humanoid") then
		local Humanoid = hit.Parent.Humanoid
		if Humanoid ~= nil then
			Humanoid.Health = Humanoid.Health - Damage.Value
		end
	end
    debounce = false
end)

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