Make a vehicle fall apart

I have a basic vehicle in my game: a base part, 4 wheels, vehicle seat. I would like the vehicle to fall apart or have the pieces explode outward when the player drives over a particular block. I tried :Destroy() but that just makes the vehicle disappear. The occasion lacks a “haha your vehicle fell apart” moment. Is there another function that would accomplish making the vehicle fall to pieces?

If your vehicle you made is held together with welds you can loop thorough the car and use :Destroy on them to make the car fall apart.

Looping through and destroying the welds makes sense. I attempted this, but I’m failing with destroying the wheel hinge welds, called “Rotate”. The weld between the vehicle seat and base is destroyed successfully. Here’s my script attempt to destroy the welds by looping. Any suggestions on what I’m doing wrong in destroying the hinge welds named “Rotate”?

script.Parent.Touched:Connect(function(part)
local vehicleSeat = part.Parent:FindFirstChild(“VehicleSeat”)

if vehicleSeat then
    vehicleSeat.Weld:Destroy()
	local items = vehicleSeat.Parent:GetChildren()
	for i= 1,#items,1 do
		local weld = items:FindFirstChild("Rotate")    -- Error for this line: attempted to index nil
		weld:Destroy()
	end
end

end)

You can do

local Vehicle = -- Index vehicle here
for _,v in pairs(Vehicle:GetDescendants()) do
     if v:IsA("Weld") then
         v:Destroy()
     end
end

This will find all welds in the vehicle and destroy them. Please also switch the topic from building support to scripting support

I believe you can use the :BreakJoints() function for that.

Vehicle:BreakJoints()

It’s only works if the vehicle is a model, which I assume it is

2 Likes

That code is much cleaner. It did destroy the vehicleSeat weld, but it still didn’t destroy the weld between the wheels and the base. I wonder if the issue has to do with how I connected the wheels to the base. To attach the wheels to the base, I went in the property window for each wheel, then under SurfaceArea, on RightSurface I changed it to Hinge. Then in the game window I slid the wheel against the base. Done. Roblox Studio named the created weld “Rotate”. I tried replacing “Weld” in the script with “HingeConstraint”, but that didn’t destroy the weld either.

vehicleSeat.Parent:BreakJoints() made all the parts fall off. Thanks for the suggestions. I’m still not sure why the wheels wouldn’t come off by using the other suggestions.

for i= 1,#items,1 do
		local weld = items:FindFirstChild("Rotate")    -- Error for this line: attempted to index nil
		weld:Destroy()
	end

You’re trying to call :FindFirstchild() on a table. You want:

local welds = vehicleSeat.Parent
for i= 1,#items,1 do
		local weld = welds:FindFirstChild("Rotate")    -- Error for this line: attempted to index nil
		weld:Destroy()
	end

Or you could use a generic for loop:

for _, v in pairs(vehicleSeat.Parent:GetChildren()) do
    if v.Name == "Rotate" then
        v:Remove()
    end
end

I inserted the generic code at the bottom but it didn’t break the wheels from the base. Odd because the name of the weld is Rotate.

1 Like

Ah okay, that’s because the welds are children of the parts. So what you need instead is:

for _, v in pairs(vehicleSeat.Parent:GetChildren()) do
    local weld = v:FindFirstChild("Rotate")
    if weld then
        weld:Remove()
    end
end

Your script worked to remove the wheels! Question though. I had created a script almost identical to what you wrote, except I didn’t include the “if - then” condition. I basically only wrote:

for _, v in pairs(vehicleSeat.Parent:GetChildren()) do
local weld = v:FindFirstChild(“Rotate”)
weld:Remove()
end

This didn’t work, obviously. I still don’t understand why it doesn’t work and why the inclusion of your if-then statement does work. Can you clarify this for me please?

Of course!
You’ll notice in the screenshot you provided me that not all the instances inside of the “Car3” model have a “Rotate” weld inside of it. This means that when we loop through them we don’t know if we have a result from local weld = v:FindFirstChild(“Rotate”).
If there is no result then calling weld:Remove() will try and remove an instance that doesn’t exist! Therefore we use the if weld then to confirm that we actually have something to delete.

Also, kudos to you for looking into the code and trying to learn from it, as well as for asking good questions and providing loads of useful information to help people answer them. You’ll go far with this mindset

Ahh…I think I understand. Let me try explaining it back to you, to see if I do understand. In the first line of my bad code:

for _, v in pairs(vehicleSeat.Parent:GetChildren()) do

The code gets all the immediate children children under Car3 (referencing my uploaded photo). This includes the Script as well as the vehicleSeat, Part…

Then the next line of code runs:

local weld = v:FindFirstChild(“Rotate”)

This line of code will look under Script, but doesn’t find a child named “Rotate”. Nothing is assigned to the variable weld. So weld = nil. So when the next line runs:

weld:Remove()

the function errors out and stops because there’s nothing to remove. The if-then statement you provided allows for the code to run whether or not a child named “Rotate” is found. Does that explanation sound about right?