Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why isn't my fade in and fade out script working for this GUI?

Asked by
AmiracIe 175
8 years ago

CONTEXT: I have a mute icon. If the player's mouse enters the mute icon, a line of text should fade into view above the mute icon, and when the player's mouse leaves the mute icon, the line of text should fade out of view.

THE PROBLEM: The text is not fading in or out correctly. It's transparency stays at 14/15 when the mouse enters and doesn't get any lower. It needs to get to 13/15, 12/15, and so on until it reaches 0/15. Instead, the mouse enters and the transparency is 15/15 - 1/15. Then when the mouse leaves the transparency is 14/15 + 1/15.

RELEVANT CODE:

local mute = Instance.new("ImageButton")
mute.Name = "mute"
mute.Parent = screenGui
mute.Image = "http://www.roblox.com/asset/?id=154834667"
mute.ZIndex = 2
mute.BackgroundTransparency = 1
mute.Size = UDim2.new(0.025,0,0.0625625,0)
mute.Position = UDim2.new(0.005,0,0.9335,0)


local mutetext = Instance.new("TextLabel",screenGui)
mutetext.Name = "mutetext"
mutetext.Position = UDim2.new(0.005,0,0.91,0)
mutetext.BackgroundTransparency = 1
mutetext.TextXAlignment = "Left"
mutetext.Text = "Scroll/click to adjust music volume"
mutetext.TextWrapped = false
mutetext.TextTransparency = 1
mutetext.TextStrokeTransparency = 1
mutetext.TextScaled = false
mutetext.Visible = true
mutetext.Font = "Arial"
mutetext.FontSize = Enum.FontSize.Size14
mutetext.Active = true

mute.MouseEnter:connect(function() 
    inText = true -- Variable set to true to detect when the mouse has entered the mute icon
end)

mute.MouseLeave:connect(function()
    inText = false -- Variable set to false to detect when the mouse has left the mute icon
end)

transparencyConstant = 1/15 -- The increment in which the text should fade in or out

function fadeIn() -- Function used to fade the text in
    MTtransparency = mutetext.TextTransparency
    MTtransparency = MTtransparency - transparencyConstant -- Supposed to make less transparent (fade in) by subtracting 1/15 every wait()
    MTtransparency = math.max(0, MTtransparency) -- The transparency of mute text cannot be lower than 0 
    print(MTtransparency) -- Used for debugging purposes
end

function fadeOut()
    local MTtransparency = mutetext.TextTransparency
    print(MTtransparency)
    MTtransparency = MTtransparency + transparencyConstant -- Supposed to make more transparent by adding 1/15 every wait()
    MTtransparency = math.min(1, MTtransparency) -- The transparency of mute text cannot be more than 1
    print(MTtransparency) -- Used for debugging purposes
end


local fadeInandOut = coroutine.wrap(function() -- Coroutine so that other code after it can work; other code can run simultaneously 
    while wait() do -- Checks every wait()
        if inText == true then -- If the mouse is in the mute icon, inText is true. And if inText is true, then
            fadeIn() -- (cont.) fade the mute text in.
        elseif inText == false then -- If the mouse isn't in the mute icon, inText is false. And if inText is false, then
            fadeOut() -- (cont.) fade the mute text out. 
        end
    end 
end)

fadeInandOut() -- Calls the function

ADDITIONAL DETAILS: The text transparency is apparently 0.9333 when the mouse enters the mute icon. This is 1 - 1/15 -- and 1/15 is my transparencyConstant increment thingy. Also, 0.93333 is printed every wait(). This means that every time inText is true, it's doing 1-1/15. Instead, I want it to do, 1 - 1/15. 14/15 - 1/15. 13/15 - 1/15. All the way to 0, where it must stop due to the line that uses math.max. Here's a picture of what's going on: http://i.imgur.com/PHGH7ap.png

The text transparency is 1 when the mouse leaves the mute icon. This is intended and not a problem: (http://i.imgur.com/AtC0rnz.png)[http://i.imgur.com/AtC0rnz.png]

If you have any questions or concerns, I'd be happy to answer them! If you are able to answer my question sufficiently, you're the best! :)

2 answers

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

You're trying to set a variable to a property, then change the property from that variable on lines 37 and 44. THIS WILL NOT WORK!. The variable becomes static, not dynamic!

You have to define the variables as the object, then index the property from the variable.

function fadeIn() -- Function used to fade the text in
    mutetext.TextTransparency  = muteText.Tansparency - transparencyConstant
    mutetext.TextTransparency = math.max(0, muteText.Transparency)
end

function fadeOut()
    mutetext.TextTransparency  = muteText.Tansparency + transparencyConstant
    mutetext.TextTransparency = math.max(0, muteText.Transparency)
end
0
Thanks for the help! So I guess dav1000999 was partially right... I'll give him an upvote AmiracIe 175 — 8y
Ad
Log in to vote
2
Answered by
davness 376 Moderation Voter
8 years ago

1. Don't redirect values to properties, but for objects:

-- the rest of the script

transparencyConstant = 1/15 -- The increment in which the text should fade in or out

function fadeIn() -- Function used to fade the text in
    local MTtransparency = mutetext
    MTtransparency = MTtransparency.TextTransparency - transparencyConstant -- Supposed to make less transparent (fade in) by subtracting 1/15 every wait()
    MTtransparency.TextTransparency = math.max(0, MTtransparency.TextTransparency) -- The transparency of mute text cannot be lower than 0 
    print(MTtransparency) -- Used for debugging purposes
end

function fadeOut()
    local MTtransparency = mutetext
    print(MTtransparency.TextTransparency)
    MTtransparency.TextTransparency = MTtransparency.TextTransparency + transparencyConstant -- Supposed to make more transparent by adding 1/15 every wait()
    MTtransparency.TextTransparency = math.min(1, MTtransparency.TextTransparency) -- The transparency of mute text cannot be more than 1
    print(MTtransparency) -- Used for debugging purposes
end

--the rest of the script

2. Where is the loop? It will just run once!!! (I recommend using a for loop)

-- the rest of the script, dont need constant

function fadeIn() -- Function used to fade the text in
    for i = 14, 0 , -1 do
        mutetext.TextTransparency = i/15
        mutetext.TextTransparency = math.max(0, mutetext.TextTransparency) -- The transparency of mute text cannot be lower than 0 
          print(mutetext.TextTransparency) -- Used for debugging purposes
        wait()
    end
end

function fadeIn() -- Function used to fade the text in
    for i = 1,15 do
        mutetext.TextTransparency = i/15
        mutetext.TextTransparency = math.min(0, mutetext.TextTransparency) -- The transparency of mute text cannot be lower than 0 
          print(mutetext.TextTransparency) -- Used for debugging purposes
        wait()
    end
end

--the rest of the script

And i think its just that. Hope it helped.

WHOLE SCRIPT

local mute = Instance.new("ImageButton")
mute.Name = "mute"
mute.Parent = screenGui
mute.Image = "http://www.roblox.com/asset/?id=154834667"
mute.ZIndex = 2
mute.BackgroundTransparency = 1
mute.Size = UDim2.new(0.025,0,0.0625625,0)
mute.Position = UDim2.new(0.005,0,0.9335,0)


local mutetext = Instance.new("TextLabel",screenGui)
mutetext.Name = "mutetext"
mutetext.Position = UDim2.new(0.005,0,0.91,0)
mutetext.BackgroundTransparency = 1
mutetext.TextXAlignment = "Left"
mutetext.Text = "Scroll/click to adjust music volume"
mutetext.TextWrapped = false
mutetext.TextTransparency = 1
mutetext.TextStrokeTransparency = 1
mutetext.TextScaled = false
mutetext.Visible = true
mutetext.Font = "Arial"
mutetext.FontSize = Enum.FontSize.Size14
mutetext.Active = true

mute.MouseEnter:connect(function() 
    inText = true -- Variable set to true to detect when the mouse has entered the mute icon
end)

mute.MouseLeave:connect(function()
    inText = false -- Variable set to false to detect when the mouse has left the mute icon
end)

transparencyConstant = 1/15 -- The increment in which the text should fade in or out

function fadeIn() -- Function used to fade the text in
    for i = 14, 0 , -1 do
        mutetext.TextTransparency = i/15
        mutetext.TextTransparency = math.max(0, mutetext.TextTransparency) -- The transparency of mute text cannot be lower than 0 
          print(mutetext.TextTransparency) -- Used for debugging purposes
        wait()
    end
end

function fadeIn() -- Function used to fade the text in
    for i = 1,15 do
        mutetext.TextTransparency = i/15
        mutetext.TextTransparency = math.min(0, mutetext.TextTransparency) -- The transparency of mute text cannot be lower than 0 
          print(mutetext.TextTransparency) -- Used for debugging purposes
        wait()
    end
end


local fadeInandOut = coroutine.wrap(function() -- Coroutine so that other code after it can work; other code can run simultaneously 
    while wait() do -- Checks every wait()
        if inText == true then -- If the mouse is in the mute icon, inText is true. And if inText is true, then
            fadeIn() -- (cont.) fade the mute text in.
        elseif inText == false then -- If the mouse isn't in the mute icon, inText is false. And if inText is false, then
            fadeOut() -- (cont.) fade the mute text out. 
        end
    end 
end)

fadeInandOut() -- Calls the function

0
"Where is the loop? It will just run once!!!" This is actually not true. The loop is in the coroutine: the line that says "while wait() do if inText == true then fadeIn() elseif inText == false then fadeOut() end end". If you look in the output that I linked, you can see that 0.9333 is printed many times. It is being called more than once but the value does not change. '1' is also printed many tim AmiracIe 175 — 8y
0
Also, your code will cause a problem if the player's mouse enters the mute icon, then quickly leaves. The fadeIn function may not be done by the time the player's mouse leaves., and the fadeIn and fadeOut script will conflict each other. I used BlueTaslem's answer here as a guide: https://goo.gl/BgkHKi AmiracIe 175 — 8y

Answer this question