Lava brick that slowly destroy bricks

I need help with a script where when the lava brick comes into contact with another brick, that brick slowly becomes invisible and then is destroyed. Does anyone know how I can do this?

I tried with chatgpt. It works, but it only works if the player touches the brick. I want it to work for all parts, regardless of whether the player touches or not.

-- Define the lava part
local lava = script.Parent

-- Function to gradually make a brick invisible and then destroy it
local function fadeAndDestroy(targetPart)
    -- Gradually reduce the transparency over time
    for transparency = 0, 1, 0.1 do
        -- If the part still exists and is not anchored
        if targetPart and targetPart:IsDescendantOf(game.Workspace) then
            targetPart.Transparency = transparency
            wait(0.1) -- Adjust speed of fading here
        else
            break
        end
    end

    -- After becoming fully transparent, destroy the part
    if targetPart and targetPart:IsDescendantOf(game.Workspace) then
        targetPart:Destroy()
    end
end

-- Detect when the lava touches another part
lava.Touched:Connect(function(hit)
    -- Check if the hit part is not anchored and is a BasePart
    if hit:IsA("BasePart") and not hit.Anchored then
        fadeAndDestroy(hit)
    end
end)

The code does seem to be functional. One potentiality could be that the part you intend to destroy with lava is anchored, which would exclude it from the script.

Could you try the following hit detector, which omits the Anchored check?

lava.Touched:Connect(function(hit)
    -- Check if the hit part is a BasePart
    if hit:IsA("BasePart") then
        fadeAndDestroy(hit)
    end
end)
2 Likes

The script seemed to work for me. If JayzYeah32’s fix doesn’t work, make sure that the parts that touch the lava have CanTouch set to true.

1 Like