How to create an attachment directly on the raycast hit position?

I want to get one of the attachments for a beam to be on the exact position that my ray is hitting. I have tried the following script but I have learned that an attachment position is relevant to the part it is a child of so now I have no idea how to position it at the ray hit position while it is also relative to the hit part’s position.

Developerpng

Error

I have tried to get the position of the ray hit position by doing some math but I cannot figure it out honestly.
Can anyone give some guidance on this who has made beams with guns before?

function CreateTracer(plr,muzzleVelocity,Dist,pos,Host)
	
	
	-- pos = raycast.Position
	--Host = raycast.Instance
	local beam = script.Parent.Beam:Clone()
	beam.Parent = game.Workspace
	local HostAttachment = Instance.new("Attachment",Host)
	local SenderAttachment = Instance.new("Attachment",gun.Tip)
	HostAttachment.Position = pos
	
	
	beam.Attachment0 = SenderAttachment
	beam.Attachment1 = HostAttachment

Try using the WorldPosition property of the attachment. Always check the properties of an object before doing extra hacky things. I had this same issue a few months ago.

2 Likes

Oh such a simple answer! I’ll try it out in a second and if it works I’ll mark your message as solution!


Worked very well! Thanks a lot

1 Like

No problem, I hope your game does well.

2 Likes