Trying to weld an attachment model to a gun model

I’m trying to make an event in my game’s new main menu UI to where you can see attachments getting put on/off the gun you’re looking at in the gun showcase in the load-out menu. I’ve tried using a lot of different welding methods but I can never get their positions to actually weld onto the gun’s hook parts of where the attachments should be.

The hooks of both models are already positioned like this:

This my current (and best) attempt for coding this event:

local attach = game.ReplicatedStorage.Attachments:FindFirstChild(child):Clone()
local parts = attach:GetChildren()
attach.Parent = curgun

attach.PrimaryPart = attach.Handle

attach:SetPrimaryPartCFrame(curgun.Hooks.Sight.CFrame)

local hook = curgun.Hooks.Sight

for i=1, #parts do
    if parts[i]:IsA("BasePart") then
        parts[i].Anchored = false
        local weld = Instance.new("Weld")
        weld.Part0 = parts[i]
        weld.Part1 = hook
        weld.C0 = hook.CFrame:inverse()
        weld.C1 = parts[i].CFrame:inverse()
        weld.Parent = hook
    end
end

I don’t think the problem is welding, it’s just changing the position/CFrame of the attachment model’s handle part to the gun model’s hook part. Since it’s able to move the same direction when I move around the showcase gun model like this:
3aa3bb71f0bec08d19d0109123484868

This is probably a really stupid question but I’m genuinely stuck on this and there’s not a lot of documentation on welding in this specific way. Any help is greatly appreciated!

6 Likes

I was just wondering about that too! I have made a game that’s created by me and its called "Games in the tall grass’ and I have set a gun to Starter Pack but it doesn’t seem to shoot the bullets. Maybe you have to set the avatar to R6 for it to work.

1 Like

In this case, you can use a WeldConstraint. WeldConstraints will weld them with the same offset that they are when you weld them. So all you have to do is position the attachment in the correct place and then make a weldconstraint between the two parts.

2 Likes

I tried it here and it seemed to do the same thing:

local attach = game.ReplicatedStorage.Attachments:FindFirstChild(child):Clone()
local parts = attach:GetDescendants()
attach.Parent = curgun

attach.PrimaryPart = attach.Handle

attach:SetPrimaryPartCFrame(curgun.Hooks.Sight.CFrame)

local hook = curgun.Hooks.Sight

for i=1, #parts do
    if parts[i]:IsA("BasePart") then
        parts[i].Anchored = false
        local weld = Instance.new("WeldConstraint")
        weld.Part0 = parts[i]
        weld.Part1 = hook
		weld.Parent = parts[i]
    end
end
1 Like

You only want to weld the attach’s primary part to the hook.

1 Like

Like this right? Because that’s not working either:

for i=1, #parts do
    if parts[i]:IsA("BasePart") then
        parts[i].Anchored = false
        local weld = Instance.new("WeldConstraint")
        weld.Part0 = attach.PrimaryPart
        weld.Part1 = hook
		weld.Parent = parts[i]
    end
end
1 Like

No, you don’t have to weld every part in the attachment. You only need to weld the PrimaryPart.

So,

attach:SetPrimaryPartCFrame(curgun.Hooks.Sight.CFrame) -- make sure it is in the right spot

local Weld = Instance.new("WeldConstraint")
Weld.Part0 = attach.PrimaryPart
Weld.Part1 = hook
Weld.Parent = attach.PrimaryPart
1 Like

Sorry if this is a lot of annoying back and forth but I tried it here and it still didn’t work:

local attach = game.ReplicatedStorage.Attachments:FindFirstChild(child):Clone()
local parts = attach:GetDescendants()
attach.Parent = curgun

attach.PrimaryPart = attach.Handle

local hook = curgun.Hooks.Sight

attach:SetPrimaryPartCFrame(curgun.Hooks.Sight.CFrame) -- make sure it is in the right spot

local Weld = Instance.new("WeldConstraint")
Weld.Part0 = attach.PrimaryPart
Weld.Part1 = hook
Weld.Parent = attach.PrimaryPart
1 Like

What is the problem you are having right now? Is curgun the right gun?

1 Like

curgun is just a gun model that is cloned to the client’s workspace like this: image
My issue is with the attachment part like this, image
isn’t being positioned to the gun’s sight hook like this:


(the blue circle is where the sight is now, it’s welded and moves along with the gun, but it’s position is supposed to be were the red circle is)

1 Like

Are you sure you are cframing the attachment to the correct spot?

1 Like

Oh no they aren’t, they’re going all the way to the bottom of the workspace were the attachment model’s original position is:


Did I do something wrong with setting the PrimaryPartCFrame?

1 Like

image

You want these two purple squares to touch right?

CFrame the top one to the one on the gun.

1 Like

Yes, both the attachment model’s handle should be at the same position as the gun model’s sight hook.

1 Like

Make sure the gun model’s sight hook is in the right position then.

1 Like

It already is? I’m not sure if I understand the question, but something did happen when I was investigating. When I ran the event the sight hook part was moved over to the attachment model’s handle part, but not the rest of the gun. It did the same for all the other parts but not the Unions/MeshParts. Can WeldConstraint only work with BaseParts? I tried switching Part0 and Part1 but it didn’t help. There are multiple models in the gun so I tried ungrouping the models of the gun model and tried doing it that way but it still didn’t work.

Both the attachment model’s handle and gun model’s hook are in the same position, but not the rest of the gun:

1 Like

Sounds like your script is written to find the original version’s position.
Rewrite the script to find the cloned version’s position.

1 Like

Thanks everyone for your help, I fixed the issue! The hook parts were unanchored the whole time :man_facepalming:

I still ended up using this code thanks to @Nezuo for the event if anyone was wondering:

local attach = game.ReplicatedStorage.Attachments:FindFirstChild(child):Clone()
local parts = attach:GetDescendants()
attach.Parent = curgun

attach.PrimaryPart = attach.Handle

local hook = curgun.Hooks.Sight

attach:SetPrimaryPartCFrame(curgun.Hooks.Sight.CFrame) -- make sure it is in the right spot

local Weld = Instance.new("WeldConstraint")
Weld.Part0 = attach.PrimaryPart
Weld.Part1 = hook
Weld.Parent = attach.PrimaryPart
2 Likes