WeldConstraint equivalent of BasePart:MakeJoints()?

Now that the Constraints system is replacing Surfaces, I’m trying to convert my game to make full use of them instead. I have one problem though: I don’t have a stable replacement for the BasePart:MakeJoints() function that uses WeldConstraints instead of Welds. Does anyone have a good solution for this?

1 Like

There is not. You will have to manually code this into a function. If your model has a root, this could be fairly easy for you to do - define one part as your root or place to hold your welds in, then recursively go through each descendant and weld it up. That’s at a simple level; anything further is up to you.

local part1 = model.RootPart

for _, part0 in pairs(model:GetDescendants()) do
    if part0:IsA("BasePart") and not (part0 == part1) then
        local WeldConstraint = Instance.new("WeldConstraint")
        WeldConstraint.Part0 = part0
        WeldConstraint.Part1 = part1
        WeldConstraint.Parent = part0
    end
end

There’s also the constraint menu, but I don’t really use that much. It gets the job done just as well however, without the involvement of code. I figure you want code since you want to use MakeJoints.

1 Like

This is similar to what I was doing before, sort of like this:

local lastpart
local i 

while i < 12 do
       MakePart()
       i = i + 1
end

function MakePart()
      local part = Instance.new(“Part”)
      if lastpart ~= nil then
      local w = Instance.new(“WeldConstraint”)
      w.Part0 = part
      w.Part1 = lastpart
      w.Parent = part
      end
      part.Parent = workspace
      lastpart = part
end

(Note that the parts are not in a Model)
The issue I have is that it doesn’t account for ALL touching parts.

The only other thing I can think of is Part.GetTouchingParts but that’s only for physics-touching parts (intersecting). You’ll probably have to think of something different instead of trying to implement MakeJoints with WeldConstraints.

As colbert2677 said, the simplest solution would be to just iterate through all the parts you want to be welded and weld them to some root. If they are not all in a Model, it might be a good idea to put them in one since they will all be welded together anyways.
Depending on your use case, the non-scripting solution is to just select all the parts you want to weld together, and click Weld in the Constraints section in studio, and all the touching parts will create a WeldConstraint between them.

Regardless, I’d like to know more about your use case if you could share. What is the context here? Like, what parts are you trying to weld and how many. Do you need to be able to do this at run-time with a script or does a simply adding them in Studio work? This information can help us figure out how to improve our tools and documentation. Thanks!

1 Like

I’m working on an improved version of the classic Trowel tool:

When the tool is activated by the player at run-time it creates 12 new parts stacked in rows of 3, and they are each welded to each other and any other touching parts using MakeJoints.

I’m sure this behavior is possible with the use of WeldConstraints, but I’m not very sure where to start. With MakeJoints I didn’t have to define what the part was being welded to.

Hopefully that made sense, I’m not experienced with explaining things like these

Thanks for the info!

First, if you are always spawning the same 4x3 wall of parts, you could just build it, weld it together, group it into a Model, and put it in ServerStorage to clone every time someone spawns a new wall. That way you don’t need to worry about welding all the wall parts together. Then, when spawned you could pass that set of parts to JoinToOutsiders and surface welds will be created on all flat sided parts the wall touches. You could also implement something similar to JoinToOutsiders with WeldConstraints using GetTouchingParts.

Alternatively, if the wall doesn’t need to move or weld to something that moves, just anchor it!

1 Like

Thank you for your help!

I will try using JoinToOutsiders and see how that goes.

Problem solved, I ended up using workspace:FindPartOnRay() to find out what to set the WeldConstraint.Part1 to.

2 Likes