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:
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!
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.
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.
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
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
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
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
curgun is just a gun model that is cloned to the client’s workspace like this:
My issue is with the attachment part like this,
isn’t being positioned to the gun’s sight hook like this:
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:
Thanks everyone for your help, I fixed the issue! The hook parts were unanchored the whole time
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