Spreading Terrain Material (like spreading fire)

Hello everyone! :wave::slightly_smiling_face:
Right now I don’t have a script that I can’t figure out, but I’m wondering if it’s possible to create a script that creates a part and changes all the terrain that’s in this part to its material (maybe by the Terrain:ReplaceMaterial function), while also growing this part to the space surrounding it.
What I want to achieve is some kind of spreading material (such as ice or cracked lava material), just like the well-known fire part spreading script does.
Besides wondering whether this is possible, I also wonder whether it would be very laggy or not.

I hope I can get any answers, because if it’s possible then it will help me a lot in making my game.

1 Like
local part = -- create a part or reference an existing part

local function spreadMaterial(position, material)
    local terrain = workspace.Terrain
    local region = Region3.new(position - Vector3.new(5, 5, 5), position + Vector3.new(5, 5, 5))
    local materialColor = BrickColor.new(material).Color

    terrain:ReplaceMaterial(region, Enum.Material.Air, materialColor)
end

local function growPart(part)
    local size = part.Size
    local position = part.Position

    for i = 1, 10 do
        size = size + Vector3.new(1, 1, 1)
        part.Size = size

        position = position - Vector3.new(0, 0.5, 0)
        part.Position = position

        spreadMaterial(position, "Ice") -- replace "Ice" with the desired material

        wait(0.1) -- adjust the delay between each growth step if needed
    end
end

-- Call the growPart function to start the spreading effect
growPart(part)

This script creates a part and then uses the growPart function to gradually increase the size of the part and change the terrain material around it using the spreadMaterial function. You can customize the material and adjust the growth rate by modifying the script.

2 Likes

Is it also possible to get the material to spread in a circle shape, and not just a block?

U can try using raycast’s instead but it would take up more data.
Here’s my rough idea,

local Part = workspace.Part
local radius = 10

function spread(rootPosition)
    for i = 1, 360, 10 do
	    x = rootPosition.X + radius * math.cos(i)
	    z = rootPosition.Z + radius * math.cos(i)
	
        local offsetPosition = Vector3.new(x, rootPosition.Y+1, z)
        local raycast = workspace:Raycast(offsetPosition, Vector3.new(0,-2,0), RayCastParams.new())

        if not raycast then continue end
        --The replacing of the material goes here n stuff
    end
end
1 Like
local part = -- create a part or reference an existing part

local function spreadMaterial(position, material)
    local terrain = workspace.Terrain
    local radius = 5 -- adjust the radius of the circle
    local materialColor = BrickColor.new(material).Color

    for x = -radius, radius do
        for z = -radius, radius do
            local offset = Vector3.new(x, 0, z)
            local distance = offset.magnitude

            if distance <= radius then
                local newPosition = position + offset
                terrain:SetMaterialColor(newPosition, Enum.Material.Air, materialColor)
            end
        end
    end
end

local function growPart(part)
    local size = part.Size
    local position = part.Position

    for i = 1, 10 do
        size = size + Vector3.new(1, 1, 1)
        part.Size = size

        position = position - Vector3.new(0, 0.5, 0)
        part.Position = position

        spreadMaterial(position, "Ice") -- replace "Ice" with the desired material

        wait(0.1) -- adjust the delay between each growth step if needed
    end
end

-- Call the growPart function to start the spreading effect
growPart(part)

here you go

1 Like