How can i achieve a fade effect for part using trancparency?

so i have this script where when you click on the part you get a tool and some other stuff but i want to add a way where the transparency will fade away and then delete itself and the part. how can i achieve this?

i was thinking something like every 0.2 seconds it will add to the transparency until the transparency is 1 it will stop and let everything else happen, but since im very new to coding, i dont know how to do this. i would appreciate some help from anyone.

code:

local Tool = game.ServerStorage:WaitForChild("shovel")
script.Parent.MouseClick:Connect(function(plr)
	script.Parent.Parent.Parent.prop_shovel.pick_up:Play()		
		
	local ToolClone = Tool:Clone()
	ToolClone.Parent = plr.Backpack
	
			script.Parent.Parent:Destroy()
	
	
end)
3 Likes

To do this, you would need to add a loop to continuously reduce the transparency until it is fully transparent. You could adjust your script like this:

local Tool = game.ServerStorage:WaitForChild("shovel")

script.Parent.MouseClick:Connect(function(plr)
    script.Parent.Parent.Parent.prop_shovel.pick_up:Play()        
    
    local ToolClone = Tool:Clone()
    ToolClone.Parent = plr.Backpack
    
    -- Fading effect
    local part = script.Parent.Parent
    local transparencyStep = 0.1  -- Adjust the step size for the fading effect
    local fadeDelay = 0.2          -- Adjust the delay between each step
    
    -- Function to gradually decrease transparency
    local function fadeOut()
        local transparency = part.Transparency
        transparency = transparency + transparencyStep
        part.Transparency = transparency
        
        if transparency >= 1 then
            part:Destroy()  -- If transparency reaches 1, destroy the part
            return
        end
        
        wait(fadeDelay)
        fadeOut()  -- Call the function recursively to continue fading
    end
    
    fadeOut()  -- Start the fading process
    
end)

This is just a basic example, so you may need to adjust some things within the script.

1 Like

You could try and use TweenService to tween the transparency of the part.

local TweenService = game:GetService("TweenService")
local info1 = TweenInfo.new(1) -- Time, in seconds, that it will take to fade

local tool = game.ServerStorage:WaitForChild("shovel")

script.Parent.MouseClick:Connect(function(player: Player)
    script.Parent.Parent.Parent.prop_shovel.pick_up:Play()

    local tool_clone = tool:Clone()
    tool_clone.Parent = player.Backpack

    local fade_tween = TweenService.new(
        script.Parent.Parent,
        info1,
        {Transparency = 1}
    )

    fade_tween:Play()
end)

If you wanted to wait for the part to fade before continuing then you could add a task.wait() to the function as well.

task.wait(info1.Time)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.