Help With Raycasting

My goal is to make a laser attachment that doesn’t phase through walls. Ive tried a few different things with some success, but once I mounted the laser to the gun things got weird! the end point was always in a certain direction and not moving with the player or the gun.

I’ve tried playing with which values I’m using to no success.

Below I will include all my code, since most of it is taken from the tutorials roblox provides.

Essentially, I want the raycast to go no more than 100 studs in the -X direction of the local position of AttachmentA.

local A1 = script.Parent:FindFirstChild("AttachmentA")
local A2 = script.Parent:FindFirstChild("AttachmentB")
local RS = game:GetService("RunService")
	local rayOrigin = A1.WorldPosition
	local rayDirection =, A1.WorldPosition.Y, A1.WorldPosition.Z)
	local raycastParams =
	raycastParams.FilterDescendantsInstances = {script.Parent.Parent.Parent.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	if raycastResult then
		A2.WorldPosition = raycastResult.Position

Try changing

local rayDirection =, A1.WorldPosition.Y, A1.WorldPosition.Z)

To this

local rayDirection = A1.WorldCFrame.LookVector.Unit * 100

Reason why your rayDirection is malfunctioning is because WorldPosition contains only position data of A1, which makes your rayDirection always to face (-1, 0, 0) in perspective of the world.

1 Like

nope, just makes it go straight up from where the attachment is.

You have to change LookVector to Right or UpVector(…or negative values of those) since only you do know how you’ve placed attachment to the part, and where you do want the ray to be facing.

1 Like

Yep, I just read the DevForum page on it, I ended up writing XVector * -100, not sure if thats how its supposed to be done but it is working. Thanks!