Trouble with gui fading and appearing

Hey! I am having an issue. My problem is that when i click on a part it is supposed to fade in a gui for 2 seconds and then it is supposed to fade out and the part is supposed to be destroyed as well as some values will be set to true. I know i am not doing right because the way i am doing it is very tedious. nonetheless it doesnt work. Here is my code please help me out.

local ClickDetector = script.Parent.ClickDetector
flashlight = game.ReplicatedStorage.Flashlight
local sound = script.Parent.Sound

ClickDetector.MouseClick:Connect(function()
local ui = game.StarterGui.FlashLight.togflash
flashlight.Value = true
sound:Play()
ui:Destroy()
script.Parent:Destroy()
ui.TextTransparency = .95
wait()
ui.TextTransparency = .9
wait()
ui.TextTransparency = .85
wait()
ui.TextTransparency = .8
wait()
ui.TextTransparency = .75
wait()
ui.TextTransparency = .7
wait()
ui.TextTransparency = .65
wait()
ui.TextTransparency = .6
wait()
ui.TextTransparency = .55
wait()
ui.TextTransparency = .5
wait()
ui.TextTransparency = .45
wait()
ui.TextTransparency = .4
wait()
ui.TextTransparency = .35
wait()
ui.TextTransparency = .3
wait()
ui.TextTransparency = .25
wait()
ui.TextTransparency = .2
wait()
ui.TextTransparency = .15
wait()
ui.TextTransparency = .1
wait()
ui.TextTransparency = .05
wait()
ui.TextTransparency = 0
wait(2.5)
ui.TextTransparency = .05
wait()
ui.TextTransparency = .1
wait()
ui.TextTransparency = .15
wait()
ui.TextTransparency = .2
wait()
ui.TextTransparency = .25
wait()
ui.TextTransparency = .3
wait()
ui.TextTransparency = .35
wait()
ui.TextTransparency = .4
wait()
ui.TextTransparency = .45
wait()
ui.TextTransparency = .5
wait()
ui.TextTransparency = .55
wait()
ui.TextTransparency = .6
wait()
ui.TextTransparency = .65
wait()
ui.TextTransparency = .7
wait()
ui.TextTransparency = .75
wait()
ui.TextTransparency = .8
wait()
ui.TextTransparency = .85
wait()
ui.TextTransparency = .9
wait()
ui.TextTransparency = .95
wait()
ui.TextTransparency = 1

end)

Use a while loop or for loop, this is very messy.

You could use TweenService for this.

Here is an example:

-- Put this near the top of your script
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Quint)

-- Use this where you want it to fade
tweenService:Create(ui, tweenInfo, {TextTransparency = 1}):Play()

This is a lot easier to use and works 100% of the time.

Hope this helps!

@ram4c TweenService is a lot easier and smoother than using a for or while loop.

4 Likes

Thank you for your reply! but i had plugged it into my script and replaced it with my old one but it still wont show up? am i doing something wrong here? This is what the script looks like:

You said fade for 2 seconds but you have “wait()” do you think that could be messing with the script try doing “wait (1)” or whatever time you wanna do when the transparency changes.

If that’s not it try doing ui.TextTransparency = (Whatever you want it to be but add 0 not just “.65” do 0.65)

1 Like

Well yes that could probably work, but i am trying to find a better way of making it fade in and out without having to type it over and over again. It is very tedious and if there is a better way of doing it i would like to use that one over my old one.

Have you tried using a function?

1 Like

? please further explain i am new to scripting.

Basically a function is a code with lines under it that you can use to repeat that doesn’t sound like the best definition but its what I know from it.

For example

local function Changing ()
-- replace changing with what you want the functions name to be.
   ui.TextTransparency = 0.5
   wait()
   ui.TextTransparency = 0.55
   wait()
end

After that just make a few more different functions with different transparency.

The after that just say

Changing() -- That's the name of the function 

That’s all if you want keep doing it you can always change things in the code with the brackets.

The problem is that you’re defining your UI with game.StarterGui. UIs are not in StarterGui, they are cloned to the PlayerGui. Instead, you need to do

local ui = game:GetService('Players').LocalPlayer:WaitForChild('PlayerGui').FlashLight.togflash

And as @Fire540Games said, TweenService is the best way to do this.

local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(2) -- duration of the tween

local flashtween = TweenService:Create(ui, info, {TextTransparency = 1})
        flashtween:Play()

Also, make sure that your script is a localscript and running from StarterGui, it will not work in a regular script running from the Workspace.

1 Like

This appears in the output after I plugged it into my script.


This is my script please tell me if anything is wrong:

The error has nothing to do with the TweenService script.
Make sure you are defining your UIs/Parts correctly, I just took a wild guess since I don’t know the setup of your UI.

I looked at your code and I spotted some small errors. You deleted the ui and the script itself before setting the transparency and thats why there was a bit of trouble.

local ClickDetector = script.Parent.ClickDetector
flashlight = game.ReplicatedStorage.Flashlight
local sound = script.Parent.Sound
local ClickDetector = script.Parent.ClickDetector
flashlight = game.ReplicatedStorage.Flashlight
local sound = script.Parent.Sound
ClickDetector.MouseClick:Connect(function()
local ui = game.StarterGui.FlashLight.togflash
flashlight.Value = true
sound:Play()
ui:Destroy()
script.Parent:Destroy()
ui.TextTransparency = .95
wait()
ui.TextTransparency = .9
wait()

Just remove the

ui:Destroy()

and the

script.Parent:Destroy()

and your script should work.

I’ve recently made a module that will help you animate gui!
Drag, fade and some more animation too!!

This module will make you less code and you can animate your gui
only with 1 line!

heres the post if you wanted: ( giving you v2 one )

1 Like

This script is very impractical, plus it doesn’t even work because the UI is defined with game.StarterGui.

Oh, I see the problem. The gui you are changing is in StarterGui, but the guis the player sees are in Player.PlayerGui.

local playerGui = game:GetService("Players").LocalPlayer.PlayerGui

local ui = playerGui.FlashLight.togflash
1 Like