Making a structure separable and then reassembling it

First of all, I would like to thank the person reading this for taking the time. The reason I am bringing this up is that my goal is to figure out how to use a rocket launcher to destroy an existing building and then restore it to its original state after all the pieces are gone. I have average scripting skills; I learned a little from the internet and a little by memorization. I am not a professional, but I have basic knowledge. The photo below shows the map, the lobby, and the areas to be blown up. I would appreciate your help.

Do you want it to just restore the building to it’s original state after some time and destroy the existing pieces?

I think i can make a simple a simple script but if you can answer that question it would be great.

1 Like

There are many ways to do this. Here is a system that might work (I haven’t tested it, so some stuff might not work but this is an idea).

Assign an attribute to each building part procedurally that says if it is destroyed. Also add an attribute to the building model itself that says the number of parts there are, and another attribute that says the number of destroyed parts. Also, add an object value to each part saying its building (if you have multiple buildings). These will be used to determine if the building is destroyed or not. You do this all with script in workspace like

local totalparts=0
for i,v in pairs(building:GetDescendants()) do
if (v:IsA("BasePart")) then
v:SetAttribute("destroyed",false)
totalparts+=1
local o=Instance.new("ObjectValue",v)
o.Value=building
o.Name="building"
end
end
building:SetAttribute("totalparts",totalparts)
building:SetAttribute("destroyedparts",0)

Create a clone of the building during runtime (so it has all the attributes you made) and put it somewhere like ServerStorage or something. This is an untouched duplicate that you can replace the destroyed building with once it is all destroyed.

local clone=building:Clone()
clone.Parent=game.ServerStorage

Then when you shoot the rocket to the building and use an Explosion to destroy it, set the destroyed attribute to true of whatever parts it hits. And, increment the destroyedparts attribute of the building by 1. Check to see if the building’s destroyedparts is equal to totalparts and that signals that every part has been destroyed (or you can also put an error margin in case explosion.Hit misses some parts or doesn’t fire a few times). Put script that does this in the rocket or wherever.

explosion.Hit:Connect(function(part) --explosion is the rocket's explosion
local isdestroyed=part:GetAttribute("destroyed")
if isdestroyed==false then
part:SetAttribute("destroyed",true) --so it doesn't detect a part anymore once it is destroyed
part.building.Value:SetAttribute("destroyedparts",part.building.Value:GetAttribute("destroyedparts")+1)
if (part.building.Value:GetAttribute("destroyedparts")>part.building.Value:GetAttribute("totalparts")-5 then --error margin: 5. compare destroyed part number with total part number. less than 5 parts must remain for it to be considered destroyed.
regenerate()
end
end
end)

And the function regenerate deletes the building, clones the original building clone you made earlier, and puts it in workspace.

function regenerate()
local newbuilding=game.ServerStorage.building:Clone() --prefab
game.Debris:AddItem(building,0)
newbuilding.Parent=workspace
end

Some stuff can still be cleaned up here (and some might not work) but this is a general idea of what you can do.

1 Like

I want to achieve both of the goals you mentioned. One is to rebuild the building after a certain period of time, for example 240 seconds. The other is to rebuild it instantly when the building is completely destroyed. The first option is simpler, but the second is more difficult and more advanced.

Thanks, the system seems logical. I like the attribute-based tracking idea, especially the regenerate part, which is practical. I’ll test the explosion hit side; sometimes it can miss, but the general logic seems to work.

It just seems a bit confusing to me; I’d really appreciate it if you could explain it in a little more detail.

Also, I heard there’s a method where you connect all the parts in the structure with WeldConstraint and then explode them, but I’m not entirely sure how it works. There are many different methods right now, and I’m having trouble figuring out which one is correct in which situation.

Thank you for taking the time to explain it in such detail. :grinning_face: