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)