Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I listen for events from multiple children?

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

✓ Accepted answer

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!

3 comments

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~

0

One thing, where did the variable "Child" come from? Other than that, thanks for the help!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.