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

[SOLVED]Why does this part detach when it is hit with an explosion?

Asked by 6 years ago
Edited 6 years ago
function weld()
    local parts,last = {}
    local function scan(parent)
        for _,v in pairs(parent:GetChildren()) do
            if (v:IsA("BasePart")) then
                if (last) then
                    local w = Instance.new("Weld")
                    w.Name = ("%s_Weld"):format(v.Name)
                    w.Part0,w.Part1 = last,v
                    w.C0 = last.CFrame:inverse()
                    w.C1 = v.CFrame:inverse()
                    w.Parent = last
                end
                last = v
                table.insert(parts,v)
            end
            scan(v)
        end
    end
    scan(script.Parent)
    for _,v in pairs(parts) do
        v.Anchored = false
    end
end

weld()
script:Remove()

(It's supposed to be a welding script.)

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
  1. It depends on the explosion's strength
  2. It only welds this once. when the weld is removed, you cannot get it back, so i designed this piece of code
while true do
wait(0.0000001) -- if longer, once hit with explosion, weld will not be in same position
    if not workspace.Map.Crate.Core:FindFirstChild("Part_Weld") then
        function weld()
            local parts,last = {}
            local function scan(parent)
                for _,v in pairs(parent:GetChildren()) do
                    if (v:IsA("BasePart")) then
                        if (last) then
                            local w = Instance.new("Weld")
                            w.Name = ("%s_Weld"):format(v.Name)
                            w.Part0,w.Part1 = last,v
                            w.C0 = last.CFrame:inverse()
                            w.C1 = v.CFrame:inverse()
                            w.Parent = last
                        end
                        last = v
                        table.insert(parts,v)
                    end
                    scan(v)
                end
            end
            scan(script.Parent)
            for _,v in pairs(parts) do
                v.Anchored = false
            end
        end
        weld()
        print("welded!")
    end
end

--script:Remove()
Ad

Answer this question