Simple part breaking

Sooooo… sup.
I got so much help so i decided to give some back.

With spoiler blurs for people wanting to learn by themselves.

We should get started tho.

So first of all… if we want each part to break we need scripts for all parts, right?

Not exactly. You see, we use Loops (for loop to be exact) to paste code everywhere we need. (saves soooo much manual labour). Loops, click it.

This is the start setup we need. Next we build something. make sure Join Surfaces is on, and put the part is a folder.



Don’t judge.
Next the loops. We need to put the Break folder in every part.
Write something in the Script that has the Break inside it.
[spoiler]


local scr = script.Break
for _,part in pairs(script.Parent.House:GetDescendants()) do
	if part:IsA("BasePart") then
		scr:Clone().Parent = part
	end
end

script.Break:Destroy()
script:Destroy()```
[/spoiler]

So we got that down...
>What about the breaking part?

i hear you say.
So where do we start with that? Well we need something to tell out script to jump into action.
![image|245x287](upload://f9g6BLlPmRLb57BgeiU5pfCK7qx.png) 

[spoiler]

function onTouched(hit)

end

connection = script.Parent.Touched:connect(onTouched)

[/spoiler]
Empty hit function it is. So we got when to activate. Create a number value called ``HP`` inside the ``Break`` folder and another called ``MinDamage``. Set the ``HP`` to 10, and the ``MinDamage`` to 1. (can change later)
![image|237x239](upload://gyaOfEsRhLasl63kKhoaYP0WyuT.png) 
Next is the velocity of our object. (some knowledge of vectors requred).
We need to check how fast BOTH the parent part AND the touched part is going. Not where... but at what speed.

[spoiler]We need to check Part.Velocity.Magnitude[/spoiler]

[spoiler]Magnitude is... well, magnitude. it combines the x,y and z coordinates.[/spoiler]
``eg.: x = 10, y = 5, z = 0 then magnitude is 15`` (in studs per second)
[spoiler]

function onTouched(hit)
if script.Parent.Parent.Velocity.Magnitude - hit.Velocity.Magnitude > script.Parent.MinDamage.Value then

end

end

connection = script.Parent.Parent.Touched:connect(onTouched)

[/spoiler]

But oh no... what if the magnitude is eg.: ``-10``? We check for that.
[spoiler]

function onTouched(hit)
if script.Parent.Parent.Velocity.Magnitude - hit.Velocity.Magnitude > script.Parent.MinDamage.Value then

elseif script.Parent.Parent.Velocity.Magnitude - hit.Velocity.Magnitude < script.Parent.MinDamage.Value * -1 then
	
end

end

connection = script.Parent.Parent.Touched:connect(onTouched)

[/spoiler]
And we need to damage the ``Part`` somehow. Why don't we take the difference between the 2 ``Part``'s ``Magnitude``s. And also parent the "dead"``Part`` to workspace, and add a timer to disapear.

[spoiler]

function onTouched(hit)
if script.Parent.Parent.Velocity.Magnitude - hit.Velocity.Magnitude > script.Parent.MinDamage.Value then
script.Parent.HP.Value = script.Parent.HP.Value - (script.Parent.Parent.Velocity.Magnitude - hit.Velocity.Magnitude)
elseif script.Parent.Parent.Velocity.Magnitude - hit.Velocity.Magnitude < script.Parent.MinDamage.Value * -1 then
script.Parent.HP.Value = script.Parent.HP.Value - ((script.Parent.Parent.Velocity.Magnitude - hit.Velocity.Magnitude) * -1)
end
if script.Parent.HP.Value <= 0 then
script.Parent.Parent:BreakJoints()
script.Parent.Parent.Parent = workspace
wait(1)
script.Parent.Parent:Destroy()
end
end

connection = script.Parent.Parent.Touched:connect(onTouched)

[/spoiler]

Now grab your high velocity slingshot and shoot away.

![](upload://xRxWqaSf0lbbwPJ5rt9UsV6fuLk.gif)

Or in my case touch it. (set realistic values)

At this point it functions perfectly. You can leave... unless you want different things to have different ``HP``s.
[spoiler]
Simple. Group them togheter in a folder like so.

![image|182x143](upload://wPtHdtIswn2PTCgqGsDynPN9Eyl.png) 

Duplicate the scripts.

![image|220x167](upload://3aAJ6VnIcLZsZXS1guYrNvQozeH.png) 

rewrite the part where it places the scripts in.

wait(2)

local scr = script.Break

for _,part in pairs(script.Parent.House:GetDescendants()) do – this part right here

if part:IsA(“BasePart”) then

scr:Clone().Parent = part

end

end

script.Break:Destroy()

script:Destroy()

[/spoiler]
Not efficient, but simple. :grinning:

Side note: set weak values while testing.
16 Likes

Instead of doubling the amount of code by having two “if” statements, you can wrap the left side of the expression in math.abs() to get the absolute value (always positive) of the magnitude.

Also, regarding different HPs: Again, it’s best to avoid duplicating code. You could add a ValueObject to each folder that holds an HP value, and from the script check against that HP instead of using a hard-coded number.

This is very vague, you should try to make your scripts as efficient as possible.

As well as Chipio said

+1, it is very hard keeping track of duplicated scripts and things can get messy.

Anyway You Can Make It A Model?

Isn’t magnitude distance which is always positive since distance has no direction? So magnitudecan never be negative.

First of all very cool this will be helpful. Second happy birthday (theres a cake next to your name)

1 Like