Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.