Parts break easily

I have this code which breaks parts welds if they hit something too fast but for some reason, the whole model breaks from one impact.

Video:

Code:

local model = script.Parent
local Parts = model:WaitForChild("Parts")

local strength = 3

local function OnTouched(part, hit)
	local partMass = part.AssemblyMass
	local hitMass = if hit.AssemblyMass == math.huge then 0 else hit.AssemblyMass

	local partVelocity = part.AssemblyLinearVelocity * partMass
	local hitVelocity = hit.AssemblyLinearVelocity * hitMass

	local relativeVelocity = partVelocity - hitVelocity

	local force = relativeVelocity.Magnitude
	local forceThreshold = partMass * strength
	
	if force >= forceThreshold then
		part:BreakJoints()
	end
end

for i,v in ipairs(Parts:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			OnTouched(v, hit)
		end)
	end
end
2 Likes

Not a scripter, but if I had to take a guess I’d say it’s because the force that breaks one weld also passes through the part itself which in turn breaks the next weld and so on, and so on.

Try making it so the parts within the model can’t break the welds of other parts inside the model.

Yeah. I can’t think of any other way to fix this.

1 Like

In case the edit doesn’t show on my other comment I’ll also type it as a reply:

Try making it so the parts within the model can’t break the welds of other parts inside that model (i.e. an extra if statement that checks if the part is parented to the same model). It might come with its own challenges such as not breaking enough on impact, but I think it would depend on the purpose of the script.

I tried disabling CanTouch after it hits but this I didn’t like as other parts that hit it wouldn’t detect it as a hit.

Definitely try making the script check the parent of the part that hits the model, that way parts inside the model itself don’t affect each other. Hope this helps.

I think your script works the way you want it to. The method you’re using to impact the object applies it over the entire object. Try dropping it onto something smaller to see if it works.

If this doesn’t work, then don’t make it 100% guaranteed to break the weld – instead, give it like a 20% chance to break each weld, that way it’s impossible for all welds to break at once.

Is there any other calculations to get it right? Probably calculated force or the threshold wrong.

I don’t think it’s that. The problem is, all parts are moving at the same velocity as one another – and I’m assuming they all have the same mass. Since they all have the same velocity and same mass, they’ll hit with the same force.

I’m not quite sure what you’re wanting to happen. What is the ideal way you want the object to break apart? Unless you add some sort of randomness or variety to your script/parts, they will all continue to do the same thing as one another unless the area of impact is smaller than the object itself.

After parts break, their assembly mass decreases because they aren’t connected to any other parts so it goes back to its own mass. All I want is parts to break on impact but this isn’t what I like.

Right there – “After parts break” This happens only after the parts break. However, at the moment of impact, all parts break instantaneously, meaning this doesn’t affect anything. What you’re thinking would work in real life since reality doesn’t need to take the time to calculate its physics and the bonds holding materials together are elastic.

In roblox, welds are not elastic at all, thus there is no “shockwave” travelling through the object, and any force applied to the object is applied to all constituent parts simultaneously.