How would I make a function loop through all variables

So I want a function to loop through when something is being hovered over.

local example = script.Parent.example
local example = script.Parent.example
local example = script.Parent.example
local example = script.Parent.example
local example = script.Parent.example

Let’s say all the examples are UI.
I want to make a certain section of code run when any of these variables are hovered over, how would I accomplish that?

Thanks!

You could utilize a loop and for each one of those items, connect an event to a function:

Example:

local GuiLayer = script.Parent:GetChildren() -- Gets everything on the same layer as the script

local function onHover()
    -- Code
end

for _, item in ipairs(GuiLayer) do
    if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
        item.MouseEnter:Connect(onHover) -- Activates the function called "onHover" whenever any of those items are hovered over
    end
end
2 Likes

For the on hover, how would I make it find the certain/refrence thing, like

local function onHover()
item: -- do code
end

So, the for i,v in pairs loop does that. You’re able to pass through the parameter of item. So it would look like this:

local function onHover(item)
    item.Text = "What's up!"
end

for _, item in ipairs(GuiLayer) do
    if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
        item.MouseEnter:Connect(onHover(item))
    end
end
1 Like

From my understanding, you would need to create an anonymous function before sending that value through, since it’s not something automatically sent through from that event:

local function onHover(item)
    print(item.Name)
end

for _, item in ipairs(GuiLayer) do
    if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
        item.MouseEnter:Connect(function()
            onHover(item)
        end)
    end
end
1 Like