How to make a attachment be placed exactly in the place that the raycast hit?

I am trying to make a raycast beam (my first time using raycasts btw), and i want to find out how to place a attachment be exactly where the raycast hit.

local Orign = script.Parent.Position
local Direction = script.Parent.CFrame.LookVector * 100  


local RayCast = game.Workspace:Raycast(Orign, Direction)

print(RayCast.Instance.Name)

You can use a Instance.new() to make the attachment, everything else should be self explanatory.

This is a sample script:

local att = Instance.new("Attachment")
att.Position = RayCast 

I’m not sure if that’s all you need, but feel free to reply if you need. :slightly_smiling_face:

Nope, that results in a error saying: “Workspace.Part.Script:9: invalid argument #3 (Vector3 expected, got RaycastResult)”

Can you show the script of “Workspace.Part.Script”? (where the error showed.)

Here it is:

local Orign = script.Parent.Position
local Direction = script.Parent.CFrame.LookVector * 100  


local RayCast = game.Workspace:Raycast(Orign, Direction)

print(RayCast.Instance.Name)

script.Parent.Finish.Position = RayCast

Oh my bad, I thought it was a different script.

Well, you shouldn’t use a variable for the Raycast, it’ll only assign the position once, it won’t update in real time as the position changes. ( Remove the “local” and just keep it as “RayCast = game.Workspace:Raycast(Orign, Direction)” )

For the main part, try editing line 9:

script.Parent.Finish.Position = RayCast.Position
1 Like