Hey guys, so I am making an old-roblox like brick battle game, and I wanted to automate the welding. But when I try, no matter how I yield, the function :GetTouchingParts() will always return an empty table.
Code:
local rs = game:GetService("RunService")
local snaps = Instance.new("Folder")
snaps.Name = "Snaps"
snaps.Parent = script.Parent
local function snapAll()
local mstaken = tick()
for _, v in ipairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") then
print("[debug]: part detected ("..v.Name..")")
local signal = v.Touched:Connect(function() end)
local touchers = v:GetTouchingParts()
signal:Disconnect()
print("[debug]: looping")
print(touchers)
for i = 1, #touchers do
local snap = Instance.new("Snap")
snap.Part0 = v
snap.Part1 = touchers[i]
print("[debug]: snapped "..v.Name.." to "..touchers[i].Name)
touchers[i].Anchored = false
v.Anchored = false
snap.Parent = snaps
print("parented")
rs.Heartbeat:Wait()
end
print("loop complete")
end
rs.Heartbeat:Wait()
end
print("completed. took "..tostring(mstaken).." ms")
end
task.wait(10)
snapAll()
Output:
Additional Details
All parts in the model have CanCollide and CanTouch turned on. I triple checked. Also, even if I yield it, no matter what, the function still keeps returning an empty table. I tried with 3 seconds, I tried with 5 seconds, and so on.
It would be hard to answer this without understanding the environment and what you’re testing it against. If you’ve tried everything and it still isn’t working you can a) provide the game environment for more context or b) use a more up-to-date method with GetPartsBoundInBox
Interesting. See if you can discover when they are getting deleted or if they are getting deleted by tracking each part. It doesn’t look like anything in your script would cause them to do anything more than unanchor and snap. It may be related to the snap mechanics. They may just be placing the part inside of themselves.
Looking at the video again I definitely believe that my assumption is correct:
When you implement a weld or snap it will default its position to the center. The parts are not deleting themselves but they’re joining together with other parts that they are touching because you’ve added the snap to them which is causing them to conjoin with each other in each other’s center.
You need to have them retain their cframe or ignore the parts within its family.
Wait I just realized you’re only trying to weld these parts together. I have a reading issue.
Why not just do this?
function snapAll(model)
local modelParts = model:GetDescendants() -- our primary part will be the first part we see cuz im lazy
for _,s in pairs(modelParts) do
if s:IsA("BasePart") or s:IsA("UnionOperation") or s:IsA("MeshPart") then -- might be a basepart idk
local WC = Instance.new("WeldConstraint")
WC.Part0 = s
WC.Part1 = modelParts[1]
WC.Parent = s
end
end
end
idk if i did this right or not cuz i dont completely remember but give it a shot?
function snapAll(model)
for _,s in pairs(model:GetDescendants()) do
if s:IsA("BasePart") or s:IsA("UnionOperation") or s:IsA("MeshPart") then -- might be a basepart idk
for _,P in pairs(game.Workspace:GetPartBoundsInBox(s.CFrame, s.Size + Vector3.new(.1, .1, .1))) do -- give some space
if s ~= P then
local WC = Instance.new("WeldConstraint")
WC.Parent = s
WC.Part0 = s
WC.Part1 = P
end
end
end
end
end
This way they’re all welded individually so you can destroy as you desire.