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

making a sticky part that attach to other parts not working properly how do i fix?

Asked by 3 years ago
function onTouched(hit)
    if hit.Parent:FindFirstChild("Key") then
        local weld = Instance.new("Weld")
        weld.Part0 = hit
        weld.Part1 = script.Parent
        local weldClone = weld:Clone()
        weldClone.Parent = script.Parent
    end
end

script.Parent.Touched:Connect(onTouched)

here i have a script that attachs everything but i wanted it only to be a part named lets say key as you can see in line 2 it didnt work maybe im just stupid but yeah

1 answer

Log in to vote
0
Answered by
7z99 203 Moderation Voter
3 years ago
Edited 3 years ago

You should iterate through the descendants of the key and create a new WeldConstraint for each BasePart inside of it.

Something like this should work:

local function onTouched(hit) -- PS. I recommend declaring functions locally.
    local key = hit.Parent:FindFirstChild("Key")
    if key then
        for i,v in pairs(key:GetDescendants()) do
        if not v:IsA('BasePart') then
            continue
        end
            local weldConstraint = Instance.new("WeldConstraint")
        weldConstraint.Parent = script.Parent
            weldConstraint.Part0 = v
            weldConstraint.Part1 = script.Parent
        end
    end
end

script.Parent.Touched:Connect(onTouched)
Ad

Answer this question