Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make removable snow terrain?

Asked by 6 years ago

I'm trying to implement a winter season into my game. I would like to be able to paint all the terrain in my game with snow, then reset it back to normal later. I asked Roblox and they said it would involve a script.

0
You need to use http://wiki.roblox.com/index.php?title=API:Class/Terrain , specifically ReadVoxels and WriteVoxels or perhaps the other functions (like copy/paste/fill). chess123mate 5873 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Hi!

You can do this by looping through the parts inside a terrain model, identifying the grass material baseparts, and painting them white, and vice versa. You can use the GetChildren() instance to get a table of the children within the terrain model.

Note: Make sure the entire terrain map is in a model, and the parts you want turned to snowy and grassy must have the Grass material. Also, the script must be in a normal script, not a local script. Otherwise it won't work.

Firstly, we need to set the variable for the map that's going to be changed, and the variable that determines whether or not it is currently snowy. We can use the snow variable to go back and forth between snowy and grassy.

local TerrainMap = workspace.Map --Identify the terrain in the workspace.
local snow = false --Whether or not the map is currently snowy.

Now let's start with the snowy function. This will take all the grass parts and paint them to be snow. This will be done by looping through the parts of the model, and identifying the grass parts. It will then insert a BrickColorValue in the part and set it to its current color, so we change it back at any time. Then, we color the part white.

We can then change the snow variable to true once it is done. If there are models within the model, that's okay! We can loop through all the models inside the terrainmap and play the function in each one.

local function snowy(model) --"Model" is a variable of the model it is going to scan
local parts = model:GetChildren() --Get the map's children.
for i = 1,#parts do
    if parts[i]:IsA("BasePart") and parts[i].Material == Enum.Material.Grass then --The child is a part and has a grass material.
        local color = Instance.new("BrickColorValue",parts[i]) --Creates a BrickColorValue and puts it in the grass part
        color.Value = parts[i].BrickColor --Sets the value to the grass part's current color. This is so we can change it back whenever.
        parts[i].BrickColor = BrickColor.new("Institutional white") --Changes the BrickColor to Institutional white.
    elseif parts[i]:IsA("Model") then --There is a model inside the model.
        snowy(parts[i]) --Scan inside that model for children as well.
end end
snow = true --The map is now snowy.
end

Now it's time for the grassy function. This will change all the snowy parts back to grass. This will be done by looping through the model's children and identifying the grass material parts. They will then be changed back to the original color that we saved in the BrickColorValue. Afterwards, the BrickColorValues will be deleted.

We can set the snow variable to know if it's snowy before cuing the function. Then set it back to false when it is done grassing up the map.

local function grassy(model)
if snow == true then --If the map is snowy then
local parts = model:GetChildren() --Get the map's children.
for i = 1,#parts do
    if parts[i]:IsA("BasePart") and parts[i].Material == Enum.Material.Grass then --The child is a part and has a grass material.
        parts[i].BrickColor = parts[i].Value.Value --Changes the snowy part's BrickColor back to its original grass color.
        parts[i].Value:remove() --Deletes the value.
    elseif parts[i]:IsA("Model") then --The child is a model.
        grassy(parts[i]) --Scan inside the model for children.
end end end
snow = false --The map is no longer snowy.
end

Now let's add a loop that will take the map back and forth from snowy to grassy. You need to put the function and then the variable for the map being changed, also known as the TerrainMap.

while true do
    snowy(TerrainMap) --Makes the map snowy!
        wait(10)
    grassy(TerrainMap) --Makes the map grassy!
        wait(10)
end

And you're done! Here's the entire script that we made.

local TerrainMap = workspace.Map --Identify the terrain in the workspace.
local snow = false --Whether or not the map is currently snowy.

local function snowy(model)
local parts = model:GetChildren()
for i = 1,#parts do
    if parts[i]:IsA("BasePart") and parts[i].Material == Enum.Material.Grass then
        local color = Instance.new("BrickColorValue",parts[i])
        color.Value = parts[i].BrickColor
        parts[i].BrickColor = BrickColor.new("Institutional white")
    elseif parts[i]:IsA("Model") then
        snowy(parts[i])
end end
snow = true
end

local function grassy(model)
if snow == true then
local parts = model:GetChildren()
for i = 1,#parts do
    if parts[i]:IsA("BasePart") and parts[i].Material == Enum.Material.Grass then
        parts[i].BrickColor = parts[i].Value.Value
        parts[i].Value:remove()
    elseif parts[i]:IsA("Model") then 
        grassy(parts[i])
end end end
snow = false
end

while true do
    snowy(TerrainMap) --Makes the map snowy!
        wait(10)
    grassy(TerrainMap) --Makes the map grassy!
        wait(10)
end

Click "Accept Answer" if this was what you were looking for! Comment if you have any more concerns!

0
@SkeletalReality So, this can work with a normal terrain (not a part terrain) as well rigbt? All I'd have to do is make the terrain into a model? if thats even possible... EpicExcelsior 8 — 6y
Ad

Answer this question