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

Way to tell if Mouse is hovering over a TextButton, or ImageButton?

Asked by 9 years ago

This is the code I tried.

01                -- LOCAL VARIABLES --
02                Player  =game.Players.LocalPlayer;
03                Mouse   =Player:GetMouse();
04                MouseDefaultIcon    ="http://www.roblox.com/asset/?id=265193813"
05                MouseHoverIcon  =""
06                MouseClickIcon  =""
07 
08 
09Mouse.MouseEnter:connect(function()
10    if Mouse.MouseEnter.className == "TextButton" or "ImageButton" then
11        Player.Character.Health = 0
12    end
13end)

I wanted to make this so I don't have to re-script the MouseIcon every for every ImageButton or TextButton.

I did Player.Character.Health = 0 just to test if it works. It does not currently, please help! Thanks!

1 answer

Log in to vote
0
Answered by
AmiracIe 175
9 years ago

Solution: GUI elements have an event called MouseEntered(). It does exactly what the name implies; fires when the player's mouse enters the GUI object.

Make sure you're using a local script with local player. The reason for this is GUIs and the mouse are client-sided; it only pertains to the player that is running the script. The server does not control your mouse; you control your mouse.

Application:

1player = game.Players.LocalPlayer
2 
3game.PlayerGui.TextButton.MouseEnter:connect(function() -- Fires when mouse enters TextButton (or any GUI element such as ImageButton)
4    player.Character.Humanoid.Health = 0 -- Kills the player
5end)

And if you want, there's also another event called 'MouseLeave'. It does the exact opposite -- fires when the player's mouse leaves the GUI element.

Additional resources: Wiki page for MouseEnter Wiki page for MouseLeave

I hope this helps and if you have any further questions or comments, let me know! :)

Ad

Answer this question