Fading part (Slowly disappears and then slowly reappears)

So I am making an obby and I am quite new to scripting (really new) and I need help with something.

I want to make a part slowly disappear and then slowly reappear.
Now I have seen many scripts making the part slowly fade away but haven’t seen one yet that makes it slowly reappear.

Blockquote
local block = script.Parent
db = false
function onTouch()
if db == false then
db = true
for i = 1, 20 do
block.Transparency = i/20
wait(0.05)
end
block.CanCollide = false
wait(2)
block.CanCollide = true
block.Transparency = 0
db = false
end
end
block.Touched:connect(onTouch)

This is the script that I am currently using. I have it so it can slowly fade away but need help with the slowly reappear part!

Any help would be appreciated!
Thank you!

I’d probably use TweenService instead, it makes operations such as these way easier to write up.

local isAnimating = false
local tweenService = game:GetService("TweenService")

--//Configs
local REAPPEAR_TIME = 2 --//Once the part becomes invisible, after this amount of seconds it'll appear again
local FADE_TIME = 3 --//How long it takes for the part to become fully visible/invisible

local function Animate()
if isAnimating then return end

isAnimating = true

local targetProperties = {}
targetProperties.Transparency = 1

local disappearFade = tweenService:Create(script.Parent, TweenInfo.new(FADE_TIME), targetProperties)

disappearFade:Play()
disappearFade.Completed:Wait()

wait(REAPPEAR_TIME)

targetProperties.Transparency = 0

local appearFade = tweenService:Create(script.Parent, TweenInfo.new(FADE_TIME), targetProperties)

appearFade:Play()
appearFade.Completed:Wait()

isAnimating = false
end

script.Parent.Touched:Connect(Animate)
2 Likes

Always try the Tutorials in the developer.roblox.com site. There is one on tweening there as well as one on how to make a part fade in and out.

1 Like

You can post code and make it pretty by surrounding it with three backticks

```
like this
```

Anyways, you can run for loops in reverse. So you’d just do

for i = 20, 0, -1 do
    block.Transparency = i / 20
    wait(0.05)
end

P.S. @C_Sharper’s answer is a good one and is the “right” way to do this. But since you’re a beginner and you’re not making skyrim here, I think a for loop is fine in this case :slight_smile:

3 Likes

I tried the script today but it isn’t working. Do I need to include something in the brackets?

Are you getting any output? I might’ve made a quick typo.

1 Like

Screenshot 2021-02-12 at 18.46.02

Is this the Issue?

Whoops, try it now. I think I fixed it

1 Like