Need help with raycasts to the players mouse

What do you want to achieve?
I am making a heat vision script and I want to make a raycast from the player to the mouse so can properly position the beam and detects hits

What is the issue?
The beam visual jitters and bugs out Video

Client Script

local tool = script.Parent
local remote = tool:WaitForChild("Remote")
local getMouse = tool:WaitForChild("GetMouse")

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
repeat wait() until plr.Character
local char = plr.Character
local hum = char.Humanoid
hum:LoadAnimation(script.Animation):Play()

local anim
local debounce = false
local cd = 2

tool.Activated:Connect(function()
	if debounce == false then
		debounce = true
		anim = hum:LoadAnimation(script.Animation)
		anim:Play()
		
		spawn(function()
			task.wait(0.5)
			anim:AdjustSpeed(0)
		end)
		
		anim:GetMarkerReachedSignal("fire"):Connect(function()
			remote:FireServer(mouse.Hit.Position)	
		end)
	end
end)

remote.OnClientEvent:Connect(function()
	anim:AdjustSpeed()
	spawn(function()
		for i = cd,0,-1 do
			tool.Name = ""
			tool.Name = "Heat Vision".."("..i..")"
			wait(1)
		end
		tool.Name = "Heat Vision"
	end)
	wait(cd)
	debounce = false
end)

giveMouse = function()
	return mouse.Hit.Position
end
getMouse.OnClientInvoke = giveMouse

Server Script

local tool = script.Parent
local remote = tool:WaitForChild("Remote")
local getMouse = tool:WaitForChild("GetMouse")

local ss = game:GetService("ServerStorage")
local runService = game:GetService("RunService")
local effects = ss.Effects:WaitForChild("Superman")

local fireing
local moveDuration = 5
remote.OnServerEvent:Connect(function(player,mousePos)
	repeat wait() until player.Character
	local char = player.Character
	local humRoot = char.HumanoidRootPart
	local head = char.Head
	local hum = char.Humanoid
	
	humRoot.Anchored = true
	
	local connection
	spawn(function()
		wait(moveDuration)
		connection:Disconnect()
		remote:FireClient(player)
		humRoot.Anchored = false
	end)
	
	local part = Instance.new("Part",workspace)
	part.Color = Color3.fromRGB(255,65,65)
	part.Material = Enum.Material.Neon
	part.CanCollide = false
	part.Anchored = true
	
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {part, char}
	params.FilterType = Enum.RaycastFilterType.Blacklist
	
	local createBeam = function()
		local mousePos = getMouse:InvokeClient(player)
		local result = workspace:Raycast(humRoot.Position,(mousePos - humRoot.Position).Unit * 100, params)
		local dist = (head.Position - result.Position).Magnitude
		
		part.Size = Vector3.new(dist,0.25,1)
		part.CFrame = CFrame.new(0,0.2,0) * CFrame.lookAt(head.Position,result.Position) * CFrame.Angles(0,math.rad(90),0) * CFrame.new(dist/2,0,0)
	end
	connection = runService.Heartbeat:Connect(createBeam)
end)

Any help will be appreciated, Thanks!

The mouse is hitting the laser part.

Do.

local part = Instance.new("Part",workspace)
	part.Color = Color3.fromRGB(255,65,65)
	part.Material = Enum.Material.Neon
	part.CanCollide = false
	part.Anchored = true
+part.CanQuery = false

To prevent that from happening

1 Like

Quick and easy solution, thank you very much