Mouse hovering over GUI button

Hi devs , anyone could tell me how to do smth like this ?

( Like a ui can follow the player mouse + changing text depend of what button the player mouse is on )

-Tnks !

1 Like

You should probably use Player:GetMouse() for this.

Then you can just set the offset of the position of the gui next to the mouse using the mouse’s ViewSizeX and ViewSizeY and update it whenever the mouse moves with Mouse:Move()

As for changing what the text says when you hover over a button, just use Gui:MouseEnter() and Gui:MouseLeave() and have it change the UI’s text/transparency.

Here is the api reference articles if you want to see the rest of the properties of the methods, or if you need to look at examples.

https://create.roblox.com/docs/reference/engine/classes/GuiObject

You can use the MouseEnter and MouseLeave events of a GUI button in Roblox Lua to detect when the user’s mouse cursor enters or leaves the button. Here’s an example script that prints a message to the output window when the user hovers over a button:

-- Define the GUI button
local button = script.Parent.Button

-- Connect the MouseEnter and MouseLeave events
button.MouseEnter:Connect(function()
    print("Mouse entered button!")
end)

button.MouseLeave:Connect(function()
    print("Mouse left button!")
end)

This script assumes that the button is a child of the script’s parent object. Replace Button with the name of your button if it has a different name.

You can modify the code inside the event handlers to perform any actions you want when the user hovers over the button. For example, you could change the button’s color or text, or show a tooltip or help message.

1 Like

Mouse enter and mouse leave won’t follow the mouse player

That’s correct. The MouseEnter and MouseLeave events are triggered when the user’s mouse cursor enters or leaves the boundaries of the GUI button, not when the cursor moves over the button. So the events won’t follow the mouse cursor as it moves around the screen.

If you want to track the mouse movement and perform actions based on its position relative to the button, you can use the MouseMoved event instead. Here’s an example script that changes the color of a button when the user hovers over it:

-- Define the GUI button
local button = script.Parent.Button

-- Connect the MouseMoved event
button.MouseMoved:Connect(function(x, y)
-- Get the button's screen space boundaries
local buttonPos = button.AbsolutePosition
local buttonSize = button.AbsoluteSize
local buttonLeft = buttonPos.X
local buttonRight = buttonPos.X + buttonSize.X
local buttonTop = buttonPos.Y
local buttonBottom = buttonPos.Y + buttonSize.Y
-- Check if the mouse is inside the button
if x >= buttonLeft and x <= buttonRight and y >= buttonTop and y <= buttonBottom then
    -- Change the button's color
    button.BackgroundColor3 = Color3.new(1, 0, 0) -- red
else
    -- Change the button's color back to the original color
    button.BackgroundColor3 = Color3.new(1, 1, 1) -- white
end
end)

This script gets the screen space boundaries of the button using the AbsolutePosition and AbsoluteSize properties, and checks if the mouse is inside the button by comparing its x and y coordinates to the button’s boundaries. If the mouse is inside the button, it changes the button’s background color to red. If the mouse is outside the button, it changes the color back to white.

You can modify the code inside the MouseMoved event handler to perform any actions you want based on the mouse’s position relative to the button.

1 Like

Yeah, but you can still use it to change whenever the text pops up, and what text it has depending on the button it is hovering.

Also this guy is definitely doing chatgpt and not checking what you are asking lol

4 Likes