How to fix my workspace:raycast() not registering

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am attempting to make a raycast gun that creates a part at a raycast intersection. I can use the old method of but since the tutorial page for it is gone I assume it is going to be deprecated in favor of workspace:Raycast(). I am attempting to make the gun register a hit.

  1. What is the issue? Include screenshots / videos if possible!

The main issue is the error i get saying the result is nil. I know what that means but I am clearly pointing the gun at the ground with the exitCFrame is looking out the guns barrel but it still is not registering a hit.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried began by looking at the documentation for workspace:Raycast() but it looks all good to me. I then went to different dev discords but since this method is new I cant find any answers.
Here is my current code I have in my module.

function RayService:CreateRay(origin, endPos, playerName)
	local raycastParams =
	raycastParams.FilterDescendantsInstances = {workspace.Baseplate}
	raycastParams.FilterType = Enum.RaycastFilterType.Whitelist

	local result = workspace:Raycast(origin, endPos, raycastParams)

	local part ="Part")
	part.Parent = workspace
	part.Anchored = true
	part.Position = result.Position

return RayService

The second argument of :Raycast is direction, not destination. This is a directional vector relative to the origin (first argument), with a length that you want to cast.

You could replace endPos with endPos - origin and likely solve your issue


thank you I will 100% try this but if it is a direction why would it change that? Wouldn’t it still intersect even if i put that as a direction?

so I just changed and that fixed the issue! Thank you very much! :smile: It is still a bit inconsistent but im pretty sure I can fix that