Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get a script to weld parts that are inside other parts?

Asked by 9 years ago

I'm making a car, and I'm making it in a way where there're parts inside of parts in a few places. The script I wrote gets them to weld, but not if they aren't inside of another part. Parts of the script are from the wiki.

local function weldBetween(a, b)
    --Make a new Weld and Parent it to a.
    if a.ClassName == "Part" or a.ClassName == "UnionOperation" then

    local weld = Instance.new("ManualWeld", a)
    weld.Part0 = a
    weld.Part1 = b
    weld.Name = "Weld"
    --Get the CFrame of b relative to a.
    weld.C0 = a.CFrame:inverse() * b.CFrame
    --Return the reference to the weld so that you can change it later.

    return weld
    end
end

for index, child in pairs(script.Parent:GetChildren()) do
    if child.ClassName == "Part" or child.ClassName == "Seat" or child.ClassName == "VehicleSeat" then
        weld1 = weldBetween(child, script.Parent.VehicleSeat)
        child.Anchored = false
    end
end

1 answer

Log in to vote
0
Answered by 9 years ago

That script will only look through the children of the model, not the descendents, to do that, you will need something along these lines:

local function weldBetween(a, b)
    --Make a new Weld and Parent it to a.
    if a.ClassName == "Part" or a.ClassName == "UnionOperation" then

    local weld = Instance.new("ManualWeld", a)
    weld.Part0 = a
    weld.Part1 = b
    weld.Name = "Weld"
    --Get the CFrame of b relative to a.
    weld.C0 = a.CFrame:inverse() * b.CFrame
    --Return the reference to the weld so that you can change it later.

    return weld
    end
end

local seat = script.Parent.VehicleSeat --Just for reference
local Model = script.Parent

function Scan(Object)
for _,Instance in pairs(Object:GetChildren()) do --Loop object's children
if Instance:IsA("BasePart") or Instance:IsA("Seat") then
weldBetween(Instance, seat)
Instance.Anchored = false
end
if (#Instance:GetChildren() > 0) then --Repeat if it has children
Scan(Instance)
end
end
end

Scan(Model)
Ad

Answer this question