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

Regenerating Models

Regenerating Models

Jul 03 2018, 9:59 AM PST 5 min

Are you making a BrickBattle game and you want to have your scenery regenerate every once in a while? It is possible to do this with scripting. This tutorial will show you how to make a model regenerate every 5 minutes and display a message to this effect.

Instructions

Make a model

The first step is to group the part of your level you want to regenerate into a model. Give your model a descriptive name. For example, in Crossroads, the part of Black Rock Castle that regenerates is grouped as a model called BlackRockCastle.

Insert a Script Into the Model

Go to Insert -> Object and when the box pops up click on the script object. This puts a script in your workspace. You should name it something descriptive like “RegenScript” (this is not strictly necessary but is a good practice to get into). Drag it into the Model you just created.

Edit the Script

Copy and paste this code into the Script you just created.

local regenTime = 180 -- 3 minutes
local messageTime = 2.5

local model = script.Parent
local message = Instance.new("Message")

--Check that the script hasn't been placed in workspace
if model ~= workspace then 
	message.Text = "Regenerating " .. model.Name

	local backup = model:clone() -- Make the backup 

	while true do 
		wait(regenTime) 

		--Remove the model
		model:Destroy()

		--Show the message
		message.Parent = game.Workspace 
		wait(messageTime)
		message.Parent = nil

		--Copy the backup
		model = backup:clone() 
		model.Parent = game.Workspace 
		model:makeJoints()
	end
else
	error("Script not put in a model! Nothing to regenerate!")
end

Save and Exit

Since scripts are constantly running, changes to them do not take effect until you exit and reload a level OR cut the script and paste it back (using the explorer tab). You should now see your model regenerating every 3 minutes.

Regeneration Buttons

To create a regeneration button, create a script, place it in the button, and copy this code into the script:

local model = game.Workspace.MyModelName

local message = Instance.new("Message")
      message.Text = "Regenerating "..model.Name
local backup = model:Clone()
local regenerating = false

function regenerate()
	--Don't regenerate again if we're already doing it!
	if regenerating then
		return
	else
		regenerating = true
	end

	model:Destroy()

	-- Display the regen message for 4 seconds
	message.Parent = game.Workspace
	wait(4)
	message.Parent = nil

	-- Put the copied model back into workspace
	model = backup:Clone()
	model.Parent = game.Workspace
	model:makeJoints()

	-- After 30 seconds, allow the model to be regenerated again
	wait(30)
	regenerating = false
end

--Connect a function that regenerates the model when a player touches the button
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		regenerate()
	end
end)

Change “MyModelName” to the name of the model you want to regenerate, and you’re done.

Tips

If you have a large map, try not to regenerate the whole thing at once, which will cause massive lag. Break your map up into a couple of sections, each of which regenerates on its own. Another thing to keep in mind is that a model is temporarily removed from the game when it is regenerating, which can cause players standing on the model to fall. Sometimes the model will then regenerate such that the player is then trapped inside of some bricks, which is bad.

If you have problems, look at Crossroads or Chaos Canyon for an example of a working regeneration script (the scripts in those maps look a little different - in particular the regen period is random - but work the same way).

Troubleshooting

Error messages: Attempt to set parent of Workspace to Workspace.Model results in circular reference: Do not group Workspace together when creating your Regen model. You can group everything else underneath Workspace.

Tags:
  • respawn
  • reset
  • script
  • coding
  • building