Regen

Regen (short for regeneration) is a method of deleting and replacing a object or other given group of instances with its copy (also known as the backup). The given group is usually cloned right after its beginning of existence and stored as the backup. Regeneration is used to restore the group that may be damaged or lost, and can be done periodically or via a button (often a part), or both.
In scripting, regeneration is one of the most used examples due to its basic elements of performing operations on the engine's data model. An example place is Crossroads, where destructible buildings are regenerated every few minutes.
== Script ==
In order to regenerate a given group of instances, specifically a object, the copy of the instance has to be stored as soon as possible. Because the given instance may not exist the time the code is executed, is used to yield the code until the instance with the given name is loaded under its parent. After it loads, the code may yield for a short period of time to allow the instance to fully load. The instance itself is then stored in the variable "model", which is used to later destroy the instance for regeneration. The code clones () the instance and stores the copy in the variable "backup".
The regen function is defined with three statements. First, it destroys () the instance stored in the variable "model". Then, it clones the instance stored in the variable "backup" and assigns it to the variable "model", which will later be destroyed when the function is called again. The copy is then parented to its original parent. This function is called every five minutes later in the example.
:
local model = workspace:WaitForChild("Model")
task.wait()
local backup = model:Clone()
local function regen()
 model:Destroy()
 model = backup:Clone()
 model.Parent = workspace
end
while task.wait(300) do
 regen()
end
=== Messages ===
In legacy scripts, regen scripts often use a object to tell visitors that something is regenerating, and adds a delay before the regeneration.
:
local model = workspace:WaitForChild("Model")
task.wait()
local backup = model:Clone()
local message = Instance.new("Message")
message.Text = "Regenerating model..."
local function regen()
 message.Parent = workspace
 
 model:Destroy()
 
 task.wait(4)
 
 model = backup:Clone()
 model.Parent = workspace
 
 message.Parent = nil
end
while task.wait(300) do
 regen()
end
=== Surface welds ===
In legacy scripts where parts are welded via surfaces, or is used to prevent the clone of the backup from falling apart after being parented.
:
-- ...
local function regen()
 message.Parent = workspace
 
 model:Destroy()
 
 task.wait(4)
 
 model = backup:Clone()
 model.Parent = workspace
 model:MakeJoints() -- makes joints
 
 message.Parent = nil
end
-- ...
=== Button ===
A regen script can be associated with a button (a part) by parenting to it and connecting the event to the regen function. Regen scripts may implement checks before calling the regen function, such as finding a object to see if it is a character that is touching the button. This is done usually in another function. 
To prevent the regen from being activated too many times, a debouncer is implemented by using an "enabled" variable which prevents the function from being called when set to false, often associated with the part's color which turns black when set to false and turns to the original color when set to true. The variable is set to false at the beginning of the function and has a delay before being set to true again.
:
local button = script.Parent
local enabled = true
local color = button.BrickColor
local model = workspace:WaitForChild("Model")
task.wait()
local backup = model:Clone()
local message = Instance.new("Message")
message.Text = "Regenerating model..."
local function regen()
 enabled = false
 button.BrickColor = BrickColor.Black()
 
 message.Parent = workspace
 
 model:Destroy()
 
 task.wait(4)
 
 model = backup:Clone()
 model.Parent = workspace
 model:MakeJoints()
 
 message.Parent = nil
 
 task.wait(30)
 
 enabled = true
 button.BrickColor = color
end
local function onTouched(hit)
 if enabled and hit.Parent:FindFirstChild("Humanoid") then
 regen()
 end
end
button.Touched:Connect(onTouched)
Category:Game development
Category:Scripting