Hey all. So i have a script that allows a part break joints when it collides at a high speed, but if i have something say like a car with multiple parts, id need to copy and paste the script into all the parts. Is there a way i can just have the script work in a model? Help would be much appreciated !
Also let me know if i can improve the code too, thanks!
deBounce = false;
BreakSpeed = 25-- adjust this for the kind of enviroment your using thi in, dont set it too low or ou cant drive/fly your model, stupid but funny if you do
KillSpeed = 10000 -- this shouldn't be lower then the break script or you may die wile driving
function onTouch(hit)
--if(hit.Name ~= "Collision Brick") then
if(deBounce == false)then
local h = hit.Parent:findFirstChild("Humanoid") or hit.Parent.Parent:findFirstChild("Humanoid")
if(h == nil) then -- not a player
deBounce = true
local collisionForce = GetImpactVelocity(hit,script.Parent);
print(collisionForce)
if(collisionForce > BreakSpeed) then
script.Parent:BreakJoints();
end
deBounce = false
else -- we've hit a player
deBounce = true;
local collisionForce = GetImpactVelocity(hit,script.Parent);
--print(collisionForce);
if(collisionForce > KillSpeed) then
--script.Parent:BreakJoints();
hit:BreakJoints();
end
deBounce = false;
end
end
--end
end
function GetImpactVelocity(part,Other) -- main function handling the forces of impact
local pPos = part.Velocity;
local oPos = Other.Velocity;
local pRspeed = {x=0,y=0,z=0}
local oRspeed = {x=0,y=0,z=0}
pRspeed.x = math.floor(pPos.x);
pRspeed.y = math.floor(pPos.y);
pRspeed.z = math.floor(pPos.z);
oRspeed.x = math.floor(oPos.x);
oRspeed.y = math.floor(oPos.y);
oRspeed.z = math.floor(oPos.z);
local IForce = {x=0,y=0,z=0}
IForce.x = pRspeed.x + -oRspeed.x
IForce.y = pRspeed.y + -oRspeed.y
IForce.z = pRspeed.z + -oRspeed.z
if(IForce.x < 0)then IForce.x = -IForce.x; end
if(IForce.y < 0)then IForce.y = -IForce.y; end
if(IForce.z < 0)then IForce.z = -IForce.z; end
local speed = math.max(IForce.x,IForce.y,IForce.z);
return speed;
end
if(script.Parent.className ~= "Script") then -- disable our break script until its inserted by the mother script
script.Parent.Touched:connect(onTouch)
end
2 Likes
At the end of your script instead of script.Parent.Touched
use:
for i,v in next, script.Parent:GetChildren() do
script.Parent.Touched:connect(onTouch)
wait()
end
but if need you can make sure its for Part class only.
1 Like
script.Parent? touch is not a valid member of a model
for i,v in next, script.Parent:GetChildren() do
script.Parent.Touched:connect(function(Touch)
onTouch(Touch)
end)
wait()
end
Also make sure the script is in a model parent, not a part parent.
1 Like
you did the same mistake. line 2, you cant have a .touched function for a model
OH I NOTICE
for i,v in next, script.Parent:GetChildren() do
v.Parent.Touched:connect(function(Touch)
onTouch(Touch)
end)
wait()
end
i frogot its v.Parent
1 Like
hey um, what would i do about these kind of lines of my script, like line 7 with like the script.parent, how would i convert it to get the parts from the model?
Or you could clone your script to all the childrens, to every part gona have his script.
example: you make a script with this:
for i,v in next, script.Parent:GetChildren() do
script.Script:Clone().Parent = v.Parent
wait()
end
and at the child of it
you put your normal script
deBounce = false;
BreakSpeed = 25-- adjust this for the kind of enviroment your using thi in, dont set it too low or ou cant drive/fly your model, stupid but funny if you do
KillSpeed = 10000 -- this shouldn't be lower then the break script or you may die wile driving
function onTouch(hit)
--if(hit.Name ~= "Collision Brick") then
if(deBounce == false)then
local h = hit.Parent:findFirstChild("Humanoid") or hit.Parent.Parent:findFirstChild("Humanoid")
if(h == nil) then -- not a player
deBounce = true
local collisionForce = GetImpactVelocity(hit,script.Parent);
print(collisionForce)
if(collisionForce > BreakSpeed) then
script.Parent:BreakJoints();
end
deBounce = false
else -- we've hit a player
deBounce = true;
local collisionForce = GetImpactVelocity(hit,script.Parent);
--print(collisionForce);
if(collisionForce > KillSpeed) then
--script.Parent:BreakJoints();
hit:BreakJoints();
end
deBounce = false;
end
end
--end
end
function GetImpactVelocity(part,Other) -- main function handling the forces of impact
local pPos = part.Velocity;
local oPos = Other.Velocity;
local pRspeed = {x=0,y=0,z=0}
local oRspeed = {x=0,y=0,z=0}
pRspeed.x = math.floor(pPos.x);
pRspeed.y = math.floor(pPos.y);
pRspeed.z = math.floor(pPos.z);
oRspeed.x = math.floor(oPos.x);
oRspeed.y = math.floor(oPos.y);
oRspeed.z = math.floor(oPos.z);
local IForce = {x=0,y=0,z=0}
IForce.x = pRspeed.x + -oRspeed.x
IForce.y = pRspeed.y + -oRspeed.y
IForce.z = pRspeed.z + -oRspeed.z
if(IForce.x < 0)then IForce.x = -IForce.x; end
if(IForce.y < 0)then IForce.y = -IForce.y; end
if(IForce.z < 0)then IForce.z = -IForce.z; end
local speed = math.max(IForce.x,IForce.y,IForce.z);
return speed;
end
if(script.Parent.className ~= "Script") then -- disable our break script until its inserted by the mother script
script.Parent.Touched:connect(onTouch)
end
So, make sure that the first script (cloner) is on the Model Child and your original script in the cloner script Child!
You can simply iterate through the children and connect your touched function
local BreakSpeed = 25-- adjust this for the kind of enviroment your using thi in, dont set it too low or ou cant drive/fly your model, stupid but funny if you do
local KillSpeed = 10000 -- this shouldn't be lower then the break script or you may die wile driving
for _, Part in next, script.Parent:GetChildren() do -- put the script in the model
if Part:IsA("BasePart") then
local Connection
Connection = Part.Touched:Connect(function(hit)
--if(hit.Name ~= "Collision Brick") then
local h = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
if(h == nil) then -- not a player
Connection:Disconnect()
local collisionForce = GetImpactVelocity(hit,Part);
print(collisionForce)
if(collisionForce > BreakSpeed) then
Part:BreakJoints();
end
else -- we've hit a player
Connection:Disconnect()
local collisionForce = GetImpactVelocity(hit,Part);
--print(collisionForce);
if(collisionForce > KillSpeed) then
--Part:BreakJoints();
hit:BreakJoints();
end
end
--end
end)
end
end
function GetImpactVelocity(part,Other) -- main function handling the forces of impact
local pPos = part.Velocity;
local oPos = Other.Velocity;
local pRspeed = {x=0,y=0,z=0}
local oRspeed = {x=0,y=0,z=0}
pRspeed.x = math.floor(pPos.x);
pRspeed.y = math.floor(pPos.y);
pRspeed.z = math.floor(pPos.z);
oRspeed.x = math.floor(oPos.x);
oRspeed.y = math.floor(oPos.y);
oRspeed.z = math.floor(oPos.z);
local IForce = {x=0,y=0,z=0}
IForce.x = pRspeed.x + -oRspeed.x
IForce.y = pRspeed.y + -oRspeed.y
IForce.z = pRspeed.z + -oRspeed.z
if(IForce.x < 0)then IForce.x = -IForce.x; end
if(IForce.y < 0)then IForce.y = -IForce.y; end
if(IForce.z < 0)then IForce.z = -IForce.z; end
local speed = math.max(IForce.x,IForce.y,IForce.z);
return speed;
end
Sorry about the poor formatting, on mobile
3 Likes
Oh god, I notice, That a lot easy than mine, Great work!
1 Like
I’ve also notice the script causes lag when the car smashes. anyway to solve it?
And also possibly make it so the parts that have fallen off dont need to “break joints”, cause they are already broken
Can i see with a gif how is your project?
sorry, cant do a gif or a video, i could say that the script tries to break parts that have already been broken, meaning that it causes lag possibly
I dont know Joints a lot but i think they may help you: JointsService | Documentation - Roblox Creator Hub
joints service has been deprecated, would it matter if i used it anyway?
Well, I cannot help you anymore on that; hopefully, someone will know better about joints than me.
1 Like
I’ve updated the script so it only runs once per part. Although, lots of parts moving at the same time is going to create lag to some extent; that’s the way the cookie crumbles
2 Likes
wow, the script doesn’t cause lag. thanks!
1 Like
hey um, theres an issue with the script where it doesn’t work after a few crashes, meaning that i have to disable it and re enable it for it to work again. Like the system isnt reliable for some reason. Know the problem?
theres also no errors too
iv’e slightly changed the script abit, meaning that something i changed could’ve done something
local BreakSpeed = 40
debris = game:GetService("Debris")
Sound = script:WaitForChild("Break1")
for _, Part in next, script.Parent:GetChildren() do
if Part:IsA("BasePart") then
local Connection
Connection = Part.Touched:Connect(function(hit)
--if(hit.Name ~= "Collision Brick") then
local h = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
if(h == nil) then -- not a player
Connection:Disconnect()
local collisionForce = GetImpactVelocity(hit,Part);
print(collisionForce)
if(collisionForce > BreakSpeed) then
Part:BreakJoints();
local S = Sound:Clone()
S.PlaybackSpeed = math.random(8,12) / 10
S.Parent = Part
S:Play()
debris:AddItem(S, 3)
debris:AddItem(Part, 10)
end
else -- we've hit a player
Connection:Disconnect()
local collisionForce = GetImpactVelocity(hit,Part);
--print(collisionForce);
end
--end
end)
end
end
function GetImpactVelocity(part,Other) -- main function handling the forces of impact
local pPos = part.Velocity;
local oPos = Other.Velocity;
local pRspeed = {x=0,y=0,z=0}
local oRspeed = {x=0,y=0,z=0}
pRspeed.x = math.floor(pPos.x);
pRspeed.y = math.floor(pPos.y);
pRspeed.z = math.floor(pPos.z);
oRspeed.x = math.floor(oPos.x);
oRspeed.y = math.floor(oPos.y);
oRspeed.z = math.floor(oPos.z);
local IForce = {x=0,y=0,z=0}
IForce.x = pRspeed.x + -oRspeed.x
IForce.y = pRspeed.y + -oRspeed.y
IForce.z = pRspeed.z + -oRspeed.z
if(IForce.x < 0)then IForce.x = -IForce.x; end
if(IForce.y < 0)then IForce.y = -IForce.y; end
if(IForce.z < 0)then IForce.z = -IForce.z; end
local speed = math.max(IForce.x,IForce.y,IForce.z);
return speed;
end