Raycasting Help from Mouse

I am looking to make a tree-cutting system by using raycasts which has been done.

I have gotten the script to work 90%, I am relatively new to raycasting but I have gone from HumanoidRootPart to mouse Viewpoint to detect whether a tree has been hit by the raycast. The issue I am facing is that on some angles whilst near the tree, the raycast does not detect the tree EVERY time. I have tried adjusting the rayDirection distance by multiplying it by a larger number, same issue occurs

See videos.

If anyone is wondering why I have repeated raycast on both client and server, I am not an expert at exploit-proofing but my general idea was that it would be a check on the server to prevent exploiters, same thing with the server debounce (works)

--- client 
local Axe = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Remote = game.ReplicatedStorage:WaitForChild("TreeCast")
local HRP = character:WaitForChild("HumanoidRootPart")
local UIS = game:GetService("UserInputService")

local mouse = player:GetMouse()
local camera = workspace.Camera

local list = {character:GetDescendants()}
local Debounce = false;
Axe.Activated:Connect(function()
	print(Debounce)
	if Debounce == false then




		local raycastParameters = RaycastParams.new()
		raycastParameters.FilterDescendantsInstances = list
		raycastParameters.FilterType = Enum.RaycastFilterType.Exclude
		local rayOrigin = Vector3.new(HRP.CFrame.LookVector,HRP.Position.Y,HRP.CFrame.LookVector)
		local rayDirection = (mouse.Hit.Position - rayOrigin).Unit * 5

		local raycastResult = workspace:Raycast(Axe.Handle.Position, rayDirection, raycastParameters)
		


		if raycastResult then
			print(raycastResult.Instance)
			if raycastResult.Instance.Name == "Tree" or raycastResult.Instance.Name == "TreeModel" then
				print("Tree hit.")
				Debounce = true;
				print('RI43')
				Remote:FireServer(Axe.Handle, character, HRP, camera, mouse.Hit.Position)
			end
		end

	else
		return
	end
end)


Remote.OnClientEvent:Connect(function(tree)
print(tree)
	Debounce = false;
	print("Client rmeote fired percent gong dwonb")
	local GUI = tree:WaitForChild("UI")
	local Percentage = GUI:WaitForChild("Percentage")
	print(Percentage)

	local Percent = Percentage.Size.X.Scale

	if Percentage.Size.X.Scale > 0 then
		Percentage.Size = UDim2.new(Percentage.Size.X.Scale - 0.35, 0,Percentage.Size.Y.Scale, 0)
	end
end)

-- server script (below)

local Remote = game.ReplicatedStorage:FindFirstChild("TreeCast")
local playerDebounce = {}
local TreeModel





Remote.OnServerEvent:Connect(function(player, Axe, character, HRP, camera, mouse)
	if not playerDebounce[player.Name] then
		playerDebounce[player.Name] = true
		local list = {}
		for _, v in pairs(game:GetService("Players"):GetPlayers()) do
			for _, q in pairs(v.Character:GetDescendants()) do
				if q:IsA("BasePart") then
					table.insert(list, v)
				end
			end
		end
		table.insert(list, Axe)
		table.insert(list, character:GetDescendants())
		local raycastParameters = RaycastParams.new()
		raycastParameters.FilterDescendantsInstances = list
		raycastParameters.FilterType = Enum.RaycastFilterType.Exclude
		local rayOrigin = Vector3.new(HRP.CFrame.LookVector.X,HRP.Position.Y,HRP.CFrame.LookVector.Z)
		local rayDirection = (mouse - rayOrigin).Unit * 5

		local raycastResult = workspace:Raycast(Axe.Position, rayDirection, raycastParameters)
		
		if raycastResult ~= nil and raycastResult.Instance.Name == "Tree" then
			print("Tree found!!")
			TreeModel = raycastResult.Instance
			wait(1)
		--	TreeModel:Destroy()
			playerDebounce[player.Name] = nil
			Remote:FireClient(player, TreeModel)

		else
			wait(1)
			playerDebounce[player.Name] = nil 
			Remote:FireClient(player, TreeModel)

			return 
		end

	end
end)


Any help would be appreciated.

I have seen that the console prints hit everytime you actually make contact with the axe and the tree by touching. Maybe you can try changing the rayDirection? Like, changing (a-b).Unit5 to (b-a).Unit5? I don’t know, let me know if that helped.

You could get when a player click and then check what instance is click by using mouse.Instance inside the event, so you wouldnt need raycast, if you wanna make a limit distance, you could use:

(mouse.hit.position - character.PrimaryPart.Position).Magnitude < maxDistance

true, but what if something is on front of it? like a wall or something?

you would need to verify what object is the mouse over to, so check if the object is the one you need