Attachment must be parented to partinstance

Hello devforum,

I’m making a raycast gun, and i’ve already run into quite a problem,

Reading this post, I know why the error is happening, but if it’s the tool itself that is a model, how would you cast from the origin of the gun barrel?

my tool tree:
hyellodevforum

my code:

local function active() -- i figure there'd be more problems with my code, so I put a little extra for you, devforum
print('e')
local attachment1 = Instance.new("Attachment", workspace)	
attachment1.Parent = tool.Barrel -- this is the piece of erroneous code
local attachment2 = Instance.new("Attachment", workspace)
attachment2.Parent = mouse.hit
local beam = Instance.new("Beam", workspace)
beam.Attachment0 = attachment1
beam.Attachment1 = mouse.hit.p
beam.Enabled = true
local raycastResult = workspace:Raycast(tool.Barrel.Position, mouse.hit.p)
print(raycastResult)
1 Like

You have to parent the attachment to the barrel without parenting it to workspace first

3 Likes

Always a pleasure to forget to tie your shoelaces :smile:

1 Like