Help with raycasting script working weirdly

I’m making a raycast gun and it is working, yet the when I visualize the raycast using a part, it doesn’t work as intended. I use the midpoint formula to move the part to the midpoint of the ray, yet I relaise that hey? Aren’t rays practically infinite? (citation needed) And the part spawns in the midpoint, yet can’t be extended farther to the gun itself.

So is there a way I can shorten a ray to a finite length? Or am I doing something scripting wise?

origin: part of the gun’s position
direction: mouse position * 10 (like a max size of the laser is 10, yet idk if i did that right)

function visualize (raycastResult)
	local midpoint = raycastResult.Origin + raycastResult.Direction/2
	local part ="Part")
	part.Anchored = true
	part.CFrame =, raycastResult.Origin)
	part.Size =,1, raycastResult.Direction.magnitude)
	part.Material = Enum.Material.Neon
	part.Color =, 1, 0)
	part.Parent = workspace



Ok weird stuff is happening. Sometimes it works and sometimes it doesnt. I’m still not sure what is wrong, I get no error messages.

Updated update:

I still need help with this /:

I’m pretty sure Gizmo can help visualize raycasts.

This is the post that someone made, it also supports raycast visualization, though you need to use an arrow if you want it to tell how far the raycast has been going.

I’m sorry what?

I don’t need random code I just need some help with my script…

Add () so it does the operation in the correct order:

local midpoint = (raycastResult.Origin + raycastResult.Direction)/2 --does the math inside the parentheses first

Also don’t confuse Ray with Raycast!

Ok ya your right about the midpoint forumula

But I still get the same resault! sometimes the rays appear to shoot out from the tool but then other times they just spawn out in space.

RaycastResult doesn’t have any of those properties. What code is calling this function?

Ya that is very misleading my bad

I thought ray was the same as raycast

So raycastResault is just


Calculate the magnitude from the origin and probably use CFrame.lookAt() instead:

local magnitude = (raycastResult.Origin - raycastResult.Direction).Magnitude

part.CFrame = CFrame.lookAt(midpoint, originPart.Position) --CFrame.lookAt() works the same as's second argument but properly made for looking, I would say it's better practice to use this instead
part.Size =,1, magnitude)

Also if these are going to be your bullets make sure to use something like PartCache as creating a lot of parts so quickly with can be very expensive.

Still not working properly

And for an example the gun only created 2 instances near it:

While these much exist:

Is the visualizing part working properly though? Is it only creating multiple parts? If so then you’re going to have to share more of your code since that means the function is being called multiple times.

Well how it works is that the function is called, by tool.activated and then ya. So sometimes I click what I describe happens above, or when I click it works as intended.