I'm trying to develop a fun tool for fun purposes as my first scripting project, and I've gotten stuck at a point where I need my script to listen for a MouseEnter event from multiple ImageButtons. I'd prefer that I keep everything in a single function if possible, instead of what I'm currently doing.
I've included some of what I have in place currently:
1 | function keyInfo(key) |
2 | local children = key:getChildren() |
3 | script.Parent.Console.Text = script.Parent.key.children [ 1 ] .Name |
4 | end |
5 |
6 | script.Parent.key.MouseEnter(keyInfo()) |
7 |
8 | -- When a key on the Gui-Keyboard is moused over, find that key and display the name of it's child on a TextLabel. |
^ I'm not expecting this to work, it's simply a messy placeholder.
This is the script I have temporarily pasted into each of the TextLabels as a workaround:
1 | function keyInfo() |
2 | if script.Parent.Name ~ = "TextLabel" then -- If the name of the TextLabel hasn't been changed, ignore it. |
3 | script.Parent.Parent.Parent.Console.Text = script.Parent.Name |
4 | else end |
5 | end |
6 |
7 | script.Parent.Parent.MouseEnter:connect(keyInfo) |
I'd rather not have to use 26 different scripts within the TextLabels, it gets messy, especially if I want to modify it.
Here is a link to Imagebin, where I've uploaded a screenshot of my Explorer window, in-case I was being unclear.
Taking all answers, and anything else you'd like to tell me.
Okay, I am just going to completely remake this whole answer for you. First, I will just show you the whole functional script, then bellow will be explanations and such.
01 | --MainScript |
02 | local Collector = script.Parent:GetChildren() |
03 | local Console = script.Parent.Console --Just a reference... |
04 |
05 | for _, Child in pairs (Collector) do |
06 | if Child.ClassName = = "ImageButton" then |
07 | Child.MouseEnter:connect( function () |
08 | Console.Text = Child.Name |
09 | end ) |
10 | end |
11 | end |
Now, What is this short code doing? It's actually doing a lot and is an effective way to shorten many events. Now, Collector is just a variable of everything inside of your GUI named Keyboard. So, We begin with a generic for loop, Child Signifying the current object being referenced by the script, this object is something that was collected by our Collector Variable.
After the start of that loop, we make a check to see if the current Child is an ImageButton, don't want any errors with something that shouldn't have a MouseEnter
event!
Then, we start our event; like I said, Child would be a ImageButton, it only fires when your mouse enters the GUI. When this event is fired, Console.Text = Child.Name
Occurs. All that line of code does is change the TextLabel's text into the name of the GUI that we are hovering over.
Please, comment again if you need more help! If this is helpful, give some rep please!