local function m(m)local C=m.Character:FindFirstChildOfClass("\084\111\111\108")if C then return C:FindFirstChild("\072\097\110\100\108\101")end end local function C(C,W,e)local U=(W.HumanoidRootPart.Position-e).Magnitude if U>MAX_HIT_PROXIMITY then return false end local h=m(C)if h then local m=(e-h.Position).Magnitude local U=(e-h.Position).Unit local Y=RaycastParams.new()Y.FilterDescendantsInstances={C.Character}local N=workspace:Raycast(h.Position,U*m,Y)if N and not N.Instance:IsDescendantOf(W)then return false end end return true end

local function getPlayerToolHandle(player)
	local weapon = player.Character:FindFirstChildOfClass("Tool")
	if weapon then
		return weapon:FindFirstChild("Handle")
	end
end

local function isHitValid(playerFired, characterToDamage, hitPosition)
	-- Validate distance between the character hit and the hit position
	local characterHitProximity = (characterToDamage.HumanoidRootPart.Position - hitPosition).Magnitude
	if characterHitProximity > MAX_HIT_PROXIMITY then
		return false
	end

	-- Check if shooting through walls
	local toolHandle = getPlayerToolHandle(playerFired)
	if toolHandle then
		local rayLength = (hitPosition - toolHandle.Position).Magnitude
		local rayDirection = (hitPosition - toolHandle.Position).Unit
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {playerFired.Character}
		local rayResult = workspace:Raycast(toolHandle.Position, rayDirection * rayLength, raycastParams)

		-- If an instance was hit that was not the character then ignore the shot
		if rayResult and not rayResult.Instance:IsDescendantOf(characterToDamage) then
			return false
		end
	end

	return true
end
