This is my first tutorial and it gives me shivers to think about the feedback. Nonetheless, here goes.
I. Briefing
Hello, fellow developers and aspiring developers. Some of you may already know how to do what I am about to explain, but I’m here to keep this on record to indoctrinate those who don’t know how, and to keep personal record for future references.
Earlier today, as I was working on a project for a group, I was creating a main menu and wanted to give my buttons the spice they require so they are not stiff or flavorless. As I was writing out the hover/leave functions for the buttons, I remembered that MouseEnter (in particular) is extremely buggy.
For a while, MouseEnter has been a problem for Roblox buttons. You just appreciate that Roblox provides MouseEnter/MouseLeave so you don’t need to spend time writing out custom logic. You get down to the testing phase, hover over your button and watch as it emits the effects you like. You click on that button, move your mouse a bit and WHOA, it’s doing something unexpected - re-running your Enter function!
So; how do you fix this? How do you get your buttons to only run MouseEnter when the mouse is truly hovered, and MouseLeave when the mouse departs from the button? Look no further, here we go!
II. Setting Up
Now the first thing you’re going to want to do is, of course, find your button(s). For the purposes of this tutorial, I’m going to smack one in the middle of the screen and hope you can follow along.
Wonderful. Now all that’s left is to get to the code. Create a LocalScript anywhere you want within the Gui, preferably in the ScreenGui since you should aim to control Guis using one script (in my opinion).
Now, I’ll be showing you two kinds of code; the one that will produce buggy behavior, and the difference if you apply the method I describe here. This will help you identify what I’m trying to convey and spot the difference.
III-I. Commonplace Method
View
In the script you made, open it up and write something. Here’s what I wrote:
Code
local Button = script.Parent:WaitForChild("TextButton")
Button.MouseEnter:Connect(function ()
Button.Text = "hovered"
end)
Button.MouseLeave:Connect(function ()
Button.Text = "left"
end)
Button.MouseButton1Click:Connect(function ()
Button.Text = "clicked"
end)
Once that’s in, exit the script editor and hit F6 on your keyboard to start a Play Solo server. Hover over your button, then hover out of it. Notice how it works as intended? Now, for the second time around, hover over your button and click it. Notice how this too work as intended? Now move your mouse just a bit. Oh no! It says “hovered” when you were already hovered over the button!
https://thumbs.gfycat.com/InfiniteMarriedIberianlynx-size_restricted.gif
This is the kind of stuff you preferably should stay away from. Sometimes it’s super annoying to look at when the button is producing improper effects.
Well then, if you don’t like that happening, look to the next method.
III-II. “Solution Method”
View
To start this off, we’re going to make a BoolValue in the TextButton. If you can handle it, you could probably put it in the script itself, but we’re not aiming for super advanced here. Give it a whacky name, edit it as you please, but it must be a descendant of the button and the value must be false. No one starts off auto-hovered over a button… right?
Next, we’re going to open our script. The bool will be serving as the main part for determining the state of the mouse (so that MouseEnter isn’t fired again after clicking), with the events only being there to support. Write some code in it along these lines:
Code
local Button = script.Parent:WaitForChild("TextButton")
local Hovered = Button:WaitForChild("Hovered")
Button.MouseEnter:Connect(function ()
if Hovered.Value == false then
Hovered.Value = true
end
end)
Button.MouseLeave:Connect(function ()
if Hovered.Value == true then
Hovered.Value = false
end
end)
Button.MouseButton1Click:Connect(function ()
Button.Text = "clicked"
end)
Hovered.Changed:Connect(function (value)
Button.Text = (value == true and "hovered" or "left")
end)
Now, follow the steps in the “Commonplace Method” for testing. Press F6 to start a Play Solo server. Hover over the button, then unhover it. Works fine? Goodie. Hover over it again and click this time, then move your mouse slightly. GASP! It doesn’t say “hovered” again!? Joy!!!
IV. Conclusion
So yeah, the bug regarding MouseEnter/MouseLeave bothered me for a while and I decided I could make a workaround by doing this. I was so overjoyed that I found a solution to my TextButton problems that I just had to share it somewhere, and the DevForum was that somewhere. I hope that you have been able to take something away from this, even though you probably already knew how to do this.
I wrote this at around 12:45 AM - 1:45 AM EST, so I may not be getting my information right or I may be providing inadequate information. Let me know if there’s a problem and I’ll address it as soon as I can.