How to stop .touched firing for a characters parts while sliding

Im trying to make a ragdoll bone breaking script.

It works, but the characters bones also break when just sliding down a part, not hitting anything.

I know it’s because the .touched fires for every it hits, but can how do i check if its an actual impact or just sliding and touching a part?

here is the script:

-- RagdollBoneReward Module
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local DataManager = require(ServerScriptService.Data:WaitForChild('DataManager'))

local HighlightRemoteEvent = ReplicatedStorage.Remotes.Misc.Ragdoll:WaitForChild("HighlightRemote")

-- SETTINGS
local BoneInfo = {
	["Head"] = {Reward = 100, UpgradeName = "Skull"},
	["Torso"] = {Reward = 50, UpgradeName = "Ribs"},
	["Left Arm"] = {Reward = 40, UpgradeName = "Arms"},
	["Right Arm"] = {Reward = 40, UpgradeName = "Arms"},
	["Left Leg"] = {Reward = 60, UpgradeName = "Legs"},
	["Right Leg"] = {Reward = 60, UpgradeName = "Legs"},
}


local HIT_COOLDOWN = 0.275
local MINIMUM_VELOCITY = 25

local hitCooldowns = {}
local activePlayers = {}
local connections = {}
local lastObjectHit = {} 

local BoneReward = {}
-- Connect Touched listeners
local function connectPartHit(player, char, part)

	local conn = part.Touched:Connect(function(hit)
		local profile = DataManager.Profiles[player]
		if not activePlayers[player.UserId] or not profile then return end
		if not hit or hit:IsDescendantOf(char) then return end
		if not BoneInfo[part.Name] then return end

		local boneName = part.Name

		-- Get last object this bone hit
		local lastObj = lastObjectHit[player.UserId][boneName]
		if lastObj == hit then
			return
		end
		lastObjectHit[player.UserId][boneName] = hit

		-- Debounce
		local key = player.UserId .. "_" .. boneName
		local lastHit = hitCooldowns[key]

		if lastHit and (time() - lastHit) < HIT_COOLDOWN then return end
		hitCooldowns[key] = time()

		-- Velocity check
		local velocity = part.AssemblyLinearVelocity.Magnitude
		if velocity < MINIMUM_VELOCITY then return end
		
		-- give reward
		local info = BoneInfo[boneName]
		local Level = profile.Data.Upgrades[info.UpgradeName] 
		local reward = math.floor(info.Reward * (1 + (0.05 * Level)))
		DataManager.AddCash(player, reward)

		-- Highlight bone
		HighlightRemoteEvent:FireClient(player, part, HIT_COOLDOWN, reward)
		
		--debug only
		--print(player.Name .. " got $" .. reward .. " from " .. boneName .. " | vel=" .. math.floor(velocity))
	end)

	table.insert(connections[player.UserId], conn)
end

-- Activate reward system
function BoneReward.Activate(player: Player, char: Model)
	if activePlayers[player.UserId] then return end

	activePlayers[player.UserId] = true
	connections[player.UserId] = {}
	lastObjectHit[player.UserId] = {} 

	for _, part in ipairs(char:GetDescendants()) do
		if part:IsA("BasePart") and BoneInfo[part.Name] then
			connectPartHit(player, char, part)
		end
	end
end


-- Deactivate reward system
function BoneReward.Deactivate(player: Player)
	if not activePlayers[player.UserId] then return end

	activePlayers[player.UserId] = nil

	if connections[player.UserId] then
		for _, conn in ipairs(connections[player.UserId]) do
			if conn and conn.Connected then
				conn:Disconnect()
			end
		end
		connections[player.UserId] = nil
	end

	lastObjectHit[player.UserId] = nil
end


return BoneReward


Something like this probably? Comments inside

-- RagdollBoneReward Module
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local DataManager = require(ServerScriptService.Data:WaitForChild('DataManager'))

local HighlightRemoteEvent = ReplicatedStorage.Remotes.Misc.Ragdoll:WaitForChild("HighlightRemote")

-- SETTINGS
local BoneInfo = {
	["Head"] = {Reward = 100, UpgradeName = "Skull"},
	["Torso"] = {Reward = 50, UpgradeName = "Ribs"},
	["Left Arm"] = {Reward = 40, UpgradeName = "Arms"},
	["Right Arm"] = {Reward = 40, UpgradeName = "Arms"},
	["Left Leg"] = {Reward = 60, UpgradeName = "Legs"},
	["Right Leg"] = {Reward = 60, UpgradeName = "Legs"},
}

local HIT_COOLDOWN = 0.275
local MINIMUM_VELOCITY = 25 -- now represents impact velocity too

local hitCooldowns = {}
local activePlayers = {}
local connections = {}
local lastObjectHit = {}

local BoneReward = {}
-- Connect Touched listeners
local function connectPartHit(player, char, part)

	-- create params once per character setup
	local rayParams = RaycastParams.new()
	rayParams.FilterType = Enum.RaycastFilterType.Exclude
	rayParams.FilterDescendantsInstances = {char} -- ignoring the character

	local conn = part.Touched:Connect(function(hit)
		local profile = DataManager.Profiles[player]
		if not activePlayers[player.UserId] or not profile then return end
		if not hit or hit:IsDescendantOf(char) then return end
		if not BoneInfo[part.Name] then return end
		
		-- Velocity check
		local velocity = part.AssemblyLinearVelocity
		if velocity.Magnitude < MINIMUM_VELOCITY then return end

		local boneName = part.Name

		-- Get last object this bone hit
		local lastObj = lastObjectHit[player.UserId][boneName]
		if lastObj == hit then return end
		lastObjectHit[player.UserId][boneName] = hit

		-- Debounce
		local key = player.UserId .. "_" .. boneName
		local lastHit = hitCooldowns[key]
		if lastHit and (time() - lastHit) < HIT_COOLDOWN then return end

		-- IMPACT CALCULATION
		-- throwing a ray in the direction we are moving to find the surface normal
		local rayDirection = velocity.Unit * 6 -- 6 studs ahead in the velocity direction
		local rayResult = workspace:Raycast(part.Position, rayDirection, rayParams)

		local impactForce = 0

		if rayResult then
			-- dot product to get the force, so if were just sliding along the part it returns zero
			-- but if we're sliding into the part it returns total speed
			-- flipping the number because Normal points out of the wall, Velocity points in
			impactForce = -velocity:Dot(rayResult.Normal)
		else
			-- raycast missing, but we have velocity so probably just sliding. return to be safe
			return 
		end

		-- check if the direct impact force is strong enough
		if impactForce < MINIMUM_VELOCITY then return end

		-- if we got here that means we got solid impact
		hitCooldowns[key] = time()

		-- give reward
		local info = BoneInfo[boneName]
		local Level = profile.Data.Upgrades[info.UpgradeName] 
		local reward = math.floor(info.Reward * (1 + (0.05 * Level)))
		DataManager.AddCash(player, reward)

		-- Highlight bone
		HighlightRemoteEvent:FireClient(player, part, HIT_COOLDOWN, reward)
		
		--debug only
		--print(player.Name .. " got $" .. reward .. " from " .. boneName .. " | vel=" .. math.floor(velocity))
	end)

	table.insert(connections[player.UserId], conn)
end

-- Activate reward system
function BoneReward.Activate(player: Player, char: Model)
	if activePlayers[player.UserId] then return end

	activePlayers[player.UserId] = true
	connections[player.UserId] = {}
	lastObjectHit[player.UserId] = {} 

	for _, part in ipairs(char:GetDescendants()) do
		if part:IsA("BasePart") and BoneInfo[part.Name] then
			connectPartHit(player, char, part)
		end
	end
end

-- Deactivate reward system
function BoneReward.Deactivate(player: Player)
	if not activePlayers[player.UserId] then return end

	activePlayers[player.UserId] = nil

	if connections[player.UserId] then
		for _, conn in ipairs(connections[player.UserId]) do
			if conn and conn.Connected then
				conn:Disconnect()
			end
		end
		connections[player.UserId] = nil
	end

	lastObjectHit[player.UserId] = nil
end

return BoneReward

You might be able to temporarily turn off CanTouch property.

For optimization reasons or why? If yes there it is. Also finded a memory leak related to hitCooldowns and fixed it

-- RagdollBoneReward Module
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local DataManager = require(ServerScriptService.Data:WaitForChild('DataManager'))

local HighlightRemoteEvent = ReplicatedStorage.Remotes.Misc.Ragdoll:WaitForChild("HighlightRemote")

-- SETTINGS
local BoneInfo = {
	["Head"] = {Reward = 100, UpgradeName = "Skull"},
	["Torso"] = {Reward = 50, UpgradeName = "Ribs"},
	["Left Arm"] = {Reward = 40, UpgradeName = "Arms"},
	["Right Arm"] = {Reward = 40, UpgradeName = "Arms"},
	["Left Leg"] = {Reward = 60, UpgradeName = "Legs"},
	["Right Leg"] = {Reward = 60, UpgradeName = "Legs"},
}


local HIT_COOLDOWN = 0.275
local MINIMUM_VELOCITY = 25

local hitCooldowns = {}
local activePlayers = {}
local connections = {}
local lastObjectHit = {} 

local BoneReward = {}
-- Connect Touched listeners
local function connectPartHit(player, char, part)

	local conn = part.Touched:Connect(function(hit)
		local profile = DataManager.Profiles[player]
		if not activePlayers[player.UserId] or not profile then return end
		if not hit or hit:IsDescendantOf(char) then return end
		if not BoneInfo[part.Name] then return end

		local boneName = part.Name

		-- Get last object this bone hit
		local lastObj = lastObjectHit[player.UserId][boneName]
		if lastObj == hit then
			return
		end
		lastObjectHit[player.UserId][boneName] = hit

		-- Debounce
		local lastHit = hitCooldowns[player.UserId][boneName]

		if lastHit and (time() - lastHit) < HIT_COOLDOWN then return end

		-- Velocity check
		local velocity = part.AssemblyLinearVelocity.Magnitude
		if velocity < MINIMUM_VELOCITY then return end
		
		hitCooldowns[player.UserId][boneName] = time()
		part.CanTouch = false -- disable touch for cooldown
		-- re-enabling Touch after cooldown
		task.delay(HIT_COOLDOWN, function()
			-- making sure part is still exists
			if part and part.Parent then
				part.CanTouch = true
			end
		end)

		-- give reward
		local info = BoneInfo[boneName]
		local Level = profile.Data.Upgrades[info.UpgradeName] 
		local reward = math.floor(info.Reward * (1 + (0.05 * Level)))
		DataManager.AddCash(player, reward)

		-- Highlight bone
		HighlightRemoteEvent:FireClient(player, part, HIT_COOLDOWN, reward)

		--debug only
		--print(player.Name .. " got $" .. reward .. " from " .. boneName .. " | vel=" .. math.floor(velocity))
	end)

	table.insert(connections[player.UserId], conn)
end

-- Activate reward system
function BoneReward.Activate(player: Player, char: Model)
	if activePlayers[player.UserId] then return end

	activePlayers[player.UserId] = true
	connections[player.UserId] = {}
	lastObjectHit[player.UserId] = {} 
	hitCooldowns[player.UserId] = {} -- for each player creating own cooldowns now

	for _, part in ipairs(char:GetDescendants()) do
		if part:IsA("BasePart") and BoneInfo[part.Name] then
			connectPartHit(player, char, part)
		end
	end
end


-- Deactivate reward system
function BoneReward.Deactivate(player: Player)
	if not activePlayers[player.UserId] then return end

	activePlayers[player.UserId] = nil

	if connections[player.UserId] then
		for _, conn in ipairs(connections[player.UserId]) do
			if conn and conn.Connected then
				conn:Disconnect()
			end
		end
		connections[player.UserId] = nil
	end

	lastObjectHit[player.UserId] = nil
	hitCooldowns[player.UserId] = nil
end


return BoneReward