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 8 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:

function keyInfo(key)
    local children = key:getChildren()
    script.Parent.Console.Text = script.Parent.key.children[1].Name
end

script.Parent.key.MouseEnter(keyInfo())

-- 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:

function keyInfo()
    if script.Parent.Name ~= "TextLabel" then-- If the name of the TextLabel hasn't been changed, ignore it.
    script.Parent.Parent.Parent.Console.Text = script.Parent.Name
    else end
end

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.

1 answer

Log in to vote
0
Answered by 8 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.

--MainScript
local Collector = script.Parent:GetChildren()
local Console = script.Parent.Console --Just a reference...

for _, Child in pairs(Collector) do
    if Child.ClassName == "ImageButton" then
        Child.MouseEnter:connect(function()
            Console.Text = Child.Name
        end)
    end
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!

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 — 8y
0
One thing, where did the variable "Child" come from? Other than that, thanks for the help! BloodyBooger 20 — 8y
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 — 8y
Ad

Answer this question