How to Make a Model Regenerate
| This is an Basic, Model related tutorial. |
Introduction
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.
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 Workspace
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).
Edit the Script
Copy and paste this code into the Script you just created. Replace "MyModelName" with the name of your model.
model = game.Workspace.MyModelName
messageText = "Regenerating MyModelName..."
message = Instance.new("Message")
message.Text = messageText
backup = model:clone()
while true do
wait(300) -- regenerate this model every 300 seconds
message.Parent = game.Workspace
model:remove()
wait(4) -- display regen message for 4 seconds
model = backup:clone()
model.Parent = game.Workspace
model:makeJoints()
message.Parent = nil
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 5 minutes.
Regeneration Buttons
To create a regeneration button, create a script, place it in the button, and copy this code into the script:
model = game.Workspace.MyModelName
messageText = "Regenerating MyModelName..."
message = Instance.new("Message")
message.Text = messageText
backup = model:clone()
enabled = true
function regenerate()
message.Parent = game.Workspace
model:remove()
wait(4) -- display regen message for 4 seconds
model = backup:clone()
model.Parent = game.Workspace
model:makeJoints()
message.Parent = nil
enabled = false
wait(30)
enabled = true
end
function onHit(hit)
if (hit.Parent:FindFirstChild("Humanoid") ~= nil) and enabled then
regenerate()
end
end
script.Parent.Touched:connect(onHit)
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).
Easy Regeneration Script
This script is very easy to use just put it in the model set the time between regenerations (if you dont want 300 secs):
---Place this INSIDE the model. Don't replace the model name or anything.
object = script.Parent
if (object ~= nil) and (object ~= game.Workspace) then
model = object
messageText = "regenerating " .. model.Name .. ""
message = Instance.new("Message")
message.Text = messageText
backup = model:clone() -- Make the backup
waitTime = 180 --Time to wait between regenerations
wait(math.random(0, waitTime))
while true do
wait(waitTime) -- Regen wait time
message.Parent = game.Workspace
model:remove()
wait(2.5) -- Display regen message for this amount of time
model = backup:clone()
model.Parent = game.Workspace
model:makeJoints()
message.Parent = nil
end
end
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.