Raycasting works strangely

So i wanted to learn raycasting and try to make a gun that tells name of the thing the ray hits,but for some reason when i aim at the baseplate or any other thing it doesnt hit anything and sometimes it does but when my cursor is not directly on the thing

Heres the script:

local Barrel = script.Parent
local RayStart = Barrel:WaitForChild("RayStart")
local Handle = Barrel.Parent
local Tool = Handle.Parent
local player = game.Players.LocalPlayer
local DealDamage = Handle:WaitForChild("DealDamage")
local Mouse = player:GetMouse()


Tool.Activated:Connect(function()
	local Params = RaycastParams.new()
	Params.FilterType = Enum.RaycastFilterType.Exclude
	Params.FilterDescendantsInstances = {Tool,Tool.Parent:GetChildren(),Barrel,Handle}
  
	local RayResults = workspace:Raycast(RayStart.Position,Mouse.Hit.Position,Params)
	if RayResults then
		if RayResults.Instance.Parent:FindFirstChild("Humanoid") then
			print(RayResults.Instance.Parent.Name)
		else
			print(RayResults.Instance.Name)
		end
	else
		print("Didnt hit anything")
	end
	
end)

Not sure if its the structure of the gun but here it is:

Thanks in advance

The second argument is the direction of the ray, Not where it should go. This is how you would actually do it.

local RayResults = workspace:Raycast(RayStart.Position,Mouse.Hit.LookVector*2048,Params)
1 Like