Hello guys, I am making a plugin for myself to weld my special tools.
The trouble;
code;
local toolbar = plugin:CreateToolbar("Gun Engine varjoy")
local changeHistory = game:GetService("ChangeHistoryService")
changeHistory:SetEnabled(true)
local newbutton = toolbar:CreateButton("Setup selected gun","Setup selected gun",'rbxassetid://5230203254')
local newbutton2 = toolbar:CreateButton("Weld selected gun","Weld selected gun",'rbxassetid://5230203254')
local function L_5(Model)
print("welding",Model.Name)
local handle = Model.Handle
for i,v in ipairs(Model.Body:GetChildren()) do
if v:IsA("BasePart") or v:IsA("MeshPart") then
local WeldC = Instance.new('Weld', Model.Body.Main)
WeldC.Name = v.Name
WeldC.Part0 = Model.Body.Main
WeldC.Part1 = v
WeldC.C0 = Model.Body.Main.CFrame:Inverse() * CFrame.new(Model.Body.Main.Position)
WeldC.C1 = v.CFrame:Inverse() * CFrame.new(Model.Body.Main.Position)
v.Anchored = false
end
end
handle.Anchored = false
local FinishWeld = Instance.new('Weld', Model.Handle)
FinishWeld.Part1 = Model.Handle
FinishWeld.Part0 = Model.Body.Main
FinishWeld.Name = 'GunMoveWeld'
Model.Handle.Anchored = false
end
newbutton2.Click:Connect(function()
local selecteds = game:GetService("Selection"):Get()
if (selecteds) then
for i,v in pairs(selecteds) do
if (v:IsA("Model") and v:FindFirstChild("Handle")) then
for a,b in pairs(v:GetDescendants()) do
if b:IsA("Weld") then
b:Destroy()
end
end
L_5(v)
changeHistory:SetWaypoint("weld Gun up")
else
warn(v.Name,'must be a model.')
end
end
else
warn("Select models to continue.")
end
end)
This is tool setup;
I have All body, welded to Main for a reason, for I can do a few animations on the gun…
FinishWeld is the weld that will be used for animation for recoil or moving a bit the gun haha
The thing is.
Handle and Main, have same size,position,orientation.
Ty for reading
Welds will automatically set the parts’ CFrame to the same. You can use WeldConstraints to replace welds, and it makes sure that the parts stay in the same relative position and orientation to one another.
I had this weld function laying around, and this might help:
local function weld(part0, part1)
local CJ = CFrame.new(part0.Position)
local weld = Instance.new("Weld")
weld.Part0 = part0
weld.Part1 = part1
weld.C0 = part0.CFrame:Inverse() * CJ
weld.C1 = part1.CFrame:Inverse() * CJ
weld.Parent = part0
end
I forgot where I got it from, but I’d like to credit the creator of this function if I can
Because the welds will set other parts’ CFrame to Model.Body.Main.CFrame, and you need to modify the C0 and C1 property to move the parts back to their original position.
Sorry, it didn’t worked
I don’t know what’s wrong
Could you please edit the code a bit?
local function Weld(Model)
local handle = Model.Handle
local CJ = CFrame.new(Model.Body.Main.Position)
local CJ2 = Model.Body.Main.CFrame:Inverse()
for i,v in ipairs(Model.Body:GetChildren()) do
if v:IsA("BasePart") or v:IsA("MeshPart") and v.Name ~= 'Main' then
local WeldC = Instance.new('Weld', Model.Body.Main)
local ToMultiply = v.CFrame:Inverse()
WeldC.Name = v.Name
WeldC.Part0 = Model.Body.Main
WeldC.Part1 = v
WeldC.C0 = CJ2 * CJ
WeldC.C1 = ToMultiply * CJ
v.Anchored = false
end
end
handle.Anchored = false
local FinishWeld = Instance.new('Weld', Model.Handle)
FinishWeld.Part1 = Model.Handle
FinishWeld.Part0 = Model.Body.Main
FinishWeld.C0 = CFrame.new(0,0,0)
FinishWeld.Name = 'GunMoveWeld'
Model.Handle.Anchored = false
end
First off, don’t use the parent property of Instance.new(), it’s bad for performance. And second, that’s the reason why it wasn’t working. Because you’ve set Part0 and Part1, the weld moved the parts and thus causing the same problem again. The fix is just to set the parent property last:
local WeldC = Instance.new('Weld')
WeldC.Name = v.Name
WeldC.Part0 = Model.Body.Main
WeldC.Part1 = v
WeldC.C0 = Model.Body.Main.CFrame:Inverse() * CJ
WeldC.C1 = v.CFrame:Inverse() * CJ
WeldC.Parent = Model.Body.Main