How can I create part at the end of a Ray?

I am trying to edit this laser script which uses ray to see if the player is looking at a wall and so then creating a part from the player to the object that is looked at to prevent it clipping through walls,

What I am trying to achieve is how can I make a 1x1x1 part get created at the end of a ray than 1 large part being the laser so then I could run a beam from the ray part straight to my gun’s part.

1 Like

Workspace::FindPartOnRay’s second return value is the end of the Ray, or where it intersected the Part.

	local direction = Handle.CFrame.LookVector.Unit
			local laserRay = Ray.new(script.Parent.LaserFrom.CFrame.p, direction * 1000) 
			local _,hitPos = workspace:FindPartOnRay(laserRay, direction, true, true)
			local dist = (script.Parent.LaserFrom.CFrame.p - hitPos).magnitude

			script.Parent.LaserTo.Position = hitPos

This my current code

The second argument has to be an Instance to ignore, you’re providing a Vector3 here.

FYI: The LookVector is already a unit vector, therefore you don’t have to get the .Unit

Try typing the ray direction with the handle CFrame position - Mouse hit position unit.

local mouse = player:GetMouse()

local laserRay = Ray.new(script.Parent.LaserFrom.CFrame.p, (mouse.Hit.p - script.Parent.LaserFrom.CFrame.p).unit  * 1000) 

local _,hitPos = workspace:FindPartOnRay(laserRay, script.Parent.LaseFrom, true, true) -- where 
the direction was in here requires an instance. it ignores the instance.
local dist = (script.Parent.LaserFrom.CFrame.p - hitPos).magnitude

script.Parent.LaserTo.Position = hitPos