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.
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()```
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.
function onTouched(hit)
end
connection = script.Parent.Touched:connect(onTouched)
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)
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.
We need to check Part.Velocity.Magnitude
Magnitude is… well, magnitude. it combines the x,y and z coordinates.
eg.: x = 10, y = 5, z = 0 then magnitude is 15
(in studs per second)
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)
But oh no… what if the magnitude is eg.: -10
? We check for that.
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)
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.
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)
Now grab your high velocity slingshot and shoot away.
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.
Simple. Group them togheter in a folder like so.
Duplicate the scripts.
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()
Not efficient, but simple.
Side note: set weak values while testing.