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

Fading a GUI button on cursor scroll?

Asked by 10 years ago

How would you do it so if you scrolled your cursor, over a GUI button it fades from the color you have it to green, or whatever you choose the color to be?

1 answer

Log in to vote
4
Answered by
nate890 495 Moderation Voter
10 years ago

By "fading" I assume you mean a "gradient" transition

The WheelForward and WheelBackward events do not fire for GUI, they only fire for the mouse object. However, we can use the mouse and WheelForward/WheelBackward events in conjunction with a GUI to determine whether or not the mouse is hovering over the GUI.

Once we figure out how to get the scroll events to work in conjunction with our GUI we need to figure out how to implement grading. I've already added a generic function that handles grading.

The following code must be in a LocalScript in order for it to function in online mode.

local mouse = game.Players.LocalPlayer:GetMouse()
local myGui = script.Parent

local debounce = false
local mouseOver = false

local colorFrom = Color3.new(0, 255, 0) -- Green (change to myGui.BackgroundColor3 for current color)
local colorTo = Color3.new(255, 0, 0) -- Red

function gradience(n, color1, color2) --a simple function that returns a color after gradience from color1 to color2  at a specific point *n* of grading. or something like that.. terminology is probably a bit off.
    local color = {0, 0, 0}
    color1 = {color1.r, color1.g, color1.b}
    color2 = {color2.r, color2.g, color2.b}
    for i = 1, 3 do
        color[i] = (color1[i] + n * (color2[i] - color1[i]))/255
    end
    return Color3.new(color[1], color[2], color[3])
end

function startTransition(i, t, start, finish) --function that changes the BackgroundColor3 of the GUI. **i** represents the amount of iterations from start to finish (value between 0 and 1) and **t** is the delay between each iteration. the smaller t is, the less choppy the transition is.
    if mouseOver and not debounce then
        debounce = true
        for n = start or 0, finish or 1, i or 0.1 do
            myGui.BackgroundColor3 = gradience(n, colorFrom, colorTo)
            wait(t or 0)
        end
        debounce = false
    end
end

myGui.MouseEnter:connect(function()
    mouseOver = true
    myGui.MouseLeave:wait()
    mouseOver = false
end)

mouse.WheelForward:connect(function()
    startTransition(0.01) --if **t** isn't given, the wait time between each transition is automatically as fast as it gets (0 or 0.03)
end)

mouse.WheelBackward:connect(function()
    startTransition(0.01)
end)

Edit: the WheelForward and WheelBackward events don't fire when the mouse is hovering over a TextButton (or ImageButton) so myGUI needs to be a TextLabel, ImageLabel or Frame.

0
Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer. User#11893 186 — 10y
0
Normally I do write some sort of explanation but I finished writing the main part of the code and was already late to leave so I just made sure it worked properly and posted it (then logged off.) nate890 495 — 10y
0
What do you mean "Edit: Textlabel or frame"?? Lem0nzz 5 — 10y
0
I mean that if you want the transition to occur when the scroll events fire then the GUI can't be a TouchButton or ImageButton because the scroll events don't fire when the mouse is hovering over them. nate890 495 — 10y
Ad

Answer this question