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

How can I listen for events from multiple children?

Asked by 9 years ago

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:

1function keyInfo(key)
2    local children = key:getChildren()
3    script.Parent.Console.Text = script.Parent.key.children[1].Name
4end
5 
6script.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:

1function 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
5end
6 
7script.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.

1 answer

Log in to vote
0
Answered by 9 years ago

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
02local Collector = script.Parent:GetChildren()
03local Console = script.Parent.Console --Just a reference...
04 
05for _, 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
11end

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!

0
I attached that Imagebin link so you can see what is a Parent of what, and it would be nice if you could explain the different steps this script would go through. Thanks~ BloodyBooger 20 — 9y
0
One thing, where did the variable "Child" come from? Other than that, thanks for the help! BloodyBooger 20 — 9y
0
The Variable "Child" can be anything if you do not already know. I just used it because everything that would be that variable is the Child of what I took children from alphawolvess 1784 — 9y
Ad

Answer this question