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 |
09 | Mouse.MouseEnter:connect( function () |
10 | if Mouse.MouseEnter.className = = "TextButton" or "ImageButton" then |
11 | Player.Character.Health = 0 |
12 | end |
13 | end ) |
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!
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:
1 | player = game.Players.LocalPlayer |
2 |
3 | game.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 |
5 | end ) |
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! :)