Make a building regenerate after an amount of damage

Hey!
I’m trying to make a building regenrate after a number of parts that are missing of it.
I know how to make the building regenrate with clone, But I can’t find the right event for it…
For example, When 50% of the parts in the building have disppeared, The building will regenerate itself to it’s original form.
How can I do that?

2 Likes

you either create a loop that checks how many parts in the building are there
or add an event for each basepart, so if the basepart’s parent changes to nil or however destruction works in your game and you run this:

local defaultAmount = 100 -- you can imput the number manually or make it count automatically with the power of scripting
local currentAmount = 0
for i, part in pairs(building:GetDescendants()) do
     if part:IsA("BasePart") then
            currentAmount = currentAmount + 1
     end
end
if currentAmount <= defaultAmount/2 then
      --regen
end
2 Likes

I know you want it to regenerate after a certain amount of damage, and there’s already a solution but I thought I’d contribute. You can change the wait time, and you can next to pairs since it does the exact same thing. The loop is how many times it checks for changes. Here you go:

local building = workspace.Building -- replace with the actual model
local newBuilding = building:Clone()
newBuilding.Parent = game:GetService("ServerStorage")

local damage = 100 -- change this for the amount added or any lost parts

while wait(60) do
	local oldList = building:GetDescendants()
	local newList = newBuilding:GetDescendants()

	local function regen()
		building:remove()
		building = newBuilding:Clone()
		building.Parent = buildingParent
	end

	local partChanges = 0
	for _, bPart in next, oldList do 
		for _,  nPart in next, newList do
			if bPart:IsA("BasePart") and nPart:IsA("BasePart") then
				if bPart.Position ~= nPart.Position then
					PartChanges = PartChanges + 1
				end
			end
		end
	end

	if #newList > #oldList then
		partChanges = partChanges + (newList - oldList)
	end

	if partChanges >= damage / 2  then
		partChanges = 0
		regen()
	end
end
2 Likes

For some reason it’s still not working…
I destroyed the whole buildings and it still does nothing

local defaultAmount = #script.Parent.GetChildren() -- you can imput the number manually or make it count automatically with the power of scripting
local currentAmount = 0
for i, part in pairs(script.Parent:GetDescendants()) do
     if part:IsA("BasePart") then
            currentAmount = currentAmount + 1
     end
end
if currentAmount <= defaultAmount/12 then
      print("regen - /12")
end

It’s not .GetChildren() it’s :GetDescendants() or :GetChildren().

2 Likes

And how can I make “defaultAmount” value count only the parts inside the model, Not the other things inside it?

You can fix it like this

function getParts(model)
	local parts = {}
	for _, part in next, model:GetDescendants() do
		if part:IsA("BasePart") then
			table.insert(parts, part)
		end
	end
	return parts;
end

-- used like this
defaultAmount = #getParts(building)
1 Like

Thanks, But it seems to still not work, Even when I changed the Parent.GetChildren() To Parent:GetChildren()

local defaultAmount = #script.Parent:GetChildren() -- you can imput the number manually or make it count automatically with the power of scripting
local currentAmount = 0
for i, part in pairs(script.Parent:GetDescendants()) do
     if part:IsA("BasePart") then
            currentAmount = currentAmount + 1
     end
end
if currentAmount <= defaultAmount/1.2 then
      print("regen - /1.2")
end

Check your console. Do some debugging yourself and attempt fixes first before writing back that the code doesn’t work if it doesn’t execute the first time around. Be as specific as you can be about how it’s not working if you report back and include your new code.

Mistakes are native to programming.

Check out the code I posted, that function returns a table which you can get the size of that table. Also, the if statement isn’t in a loop which is what the original code I posted can fix.

I tried your code, It does nothing…
In the comsole it says " Game script timeout "…

Sorry, the wait was too short. I just updated it to fix that.

It still does nothing, And still wrties the “Game script timeout…”
local building = workspace[“Apartments”] – replace with the actual model
local newBuilding = building:Clone()
newBuilding.Parent = game:GetService(“ServerStorage”)

local damage = 200 -- change this for the amount added or any lost parts

while wait(60) do
	local oldList = building:GetDescendants()
	local newList = newBuilding:GetDescendants()

	local function regen()
		building:remove()
		building = newBuilding:Clone()
		building.Parent = workspace
	end

	local partChanges = 0
	for _, bPart in next, oldList do 
		for _,  nPart in next, newList do
			if bPart:IsA("BasePart") and nPart:IsA("BasePart") then
				if bPart.Position ~= nPart.Position then
					partChanges = partChanges + 1
				end
			end
		end
	end

	if #newList > #oldList then
		partChanges = partChanges + (newList - oldList)
	end

	if partChanges >= 25  then
		partChanges = 0
		regen()
		print("doneregen")
	end
end

I guess that doesn’t work, I’m sorry for wasting your time.