Hello! I’m trying to make a weld system. The goal is to have all parts in my map, except for the baseplate, unanchored and welded together. However, the weld script I have now makes a TON of extra welds, which causes lag on larger maps. How could I improve this so it doesn’t weld parts together multiple times?
for i, child in pairs(model:GetDescendants()) do
if child:IsA("BasePart") then
for i, part in pairs(child:GetTouchingParts()) do
local weld = Instance.new("WeldConstraint")
weld.Parent = child
weld.Part0 = child
weld.Part1 = part
end
end
end
I added an extra line to your code that only welds parts that are unanchored.
for i, child in pairs(model:GetDescendants()) do
if child:IsA("BasePart") then
for i, part in pairs(child:GetTouchingParts()) do
if not part.Anchored and not part:FindFirstChild("WeldConstraint") then
local weld = Instance.new("WeldConstraint")
weld.Parent = child
weld.Part0 = child
weld.Part1 = part
end
end
end
end
Edit: I added where parts will not have more than one weld constraint.
I’m making a realistic destruction system in which the parts should be unanchored and welded. This makes it look better when welds are broken, rather than just unanchoring the part.
For example, with just anchored parts, breaking the bottom layer of a house would leave it floating. no Minecraft physics here. This is obviously wrong, it should all fall. With welds, it has the freedom to do that.
This would still weld multiple parts together. What I’m looking for is this:
Bad explanation
You have a part surrounded by 4 other parts.
[Part]
[Part][Part][Part]
[Part]
The current code would add a weld, welding that part to the other four, and all four of those back to the original part. I need some way of checking if the current part looping to is already welded to the part that it’s trying to weld to.
GOOD EXPLANATION:
You have part A, part B, and part C. This script starts with part A. It welds it to part B and part C. THEN, it goes to part B, which gets welded back to part A. You see the problem? Part A and part B are getting welded together twice. That’s not right. This would also happen with part C, and any other parts together.
You could group the parts needing to be welded and then name the part in the middle something different. You could then tag the grouped objects and do a double for loop welding the parts to the central part.
for _, child in pairs(model:GetDescendants()) do
if child:IsA("BasePart") then
for _, part in pairs(child:GetTouchingParts()) do
local isWelded = false
for _, weld in pairs(part:GetChildren()) do
if weld:IsA("WeldConstraint") then
if (weld.Part0 == child and weld.Part1 == part) or (weld.Part0 == part and weld.Part1 == child) then
isWelded = true
break
end
end
end
if not isWelded then
local weld = Instance.new("WeldConstraint")
weld.Part0 = child
weld.Part1 = part
weld.Parent = child
end
end
end
end
What this code does is essentially check that the parts are not already welded together, and if not, creates a new weld between said parts.
Sorry to bother you again. This worked in my small test, with a few parts grouped together, but in my larger test with multiple buildings, it seems to not weld some parts. My most likely culprit is :GetTouchingParts, but I can’t be sure. any advice? The parts are physically right next to each other.
EDIT: This even includes the baseplate! What the heck?
Sorry, moved this issue over to another thread. But to answer your question, they’re all anchored, can collide true, and actually touching each other. Only parts inside each other are being welded… I’ve checked.