PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Regenerate Models

Regenerate Models

Jun 23 2018, 2:01 AM PST

Problem

You want to create a button that regenerates a model.

Solution

Create a new copy of the model using the clone method.

local model, button, enabled = Workspace.Model, Workspace.Button, true
local clone = model:Clone()
 
button.Touched:connect(function(hit)
	if game.Players:FindFirstChild(hit.Parent.Name) and enabled then
		enabled = false
		model:Destroy()
		wait(5)
		local lclone = clone:Clone()
		lclone.Parent = Workspace
		model = lclone
		enabled = true
	end
end)

Discussion

This is a very simple regeneration with none of the fancy purple or black coloring many have these days, however that functionality can be added very easily. We have a model Workspace.Model that we clone and set to the variable “clone”. When you call clone on an object you clone it and all of its children and set the object’s parent to nil. We then connect the BasePart/Touched|Touched event on our regeneration button and make sure whatever hit the button was a player. We also do the standard debounce check. We then remove the original model, wait 5 seconds and then create a clone of our cloned model. We parent this clone to the Workspace and then set “model” to our newly created clone.

Think of “model” is our display and “clone” as a reference of how the model looked in the first place. When we regenerate “model”, we create a clone of our reference model (because we want to keep the reference if we want to regenerate another time) and then set that as the new display.