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

Give multiple buttons same function?

Asked by 6 years ago

I've been working on an inventory GUI, so far so good but I dont quite understand how I give multiple buttons the same function. I want all buttons to have a glow effect when the player's mouse hovers over them. I know I could give them the function individually but I feel thats not efficient.

local items = {
    item1 = itemselect.Item1,
    item2 = itemselect.Item2,
    item3 = itemselect.Item3,
    item4 = itemselect.Item4,
    item5 = itemselect.Item5,
    item6 = itemselect.Item6,
    item7 = itemselect.Item7,
    item8 = itemselect.Item8,
}


for i,v in pairs(items) do
    print(i,v)
end

I was told to use a for i,v pair loop but this still doesn't answer my question.

0
you use MouseEnter and MouseLeave in the for loop ( v.MouseEnter:Connect(function() *glow up* end) ) radusavin366 617 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

You're on the right track. All you need to do is connect a function to the MouseHover event for each button.

local items = {
    item1 = itemselect.Item1,
    item2 = itemselect.Item2,
    item3 = itemselect.Item3,
    item4 = itemselect.Item4,
    item5 = itemselect.Item5,
    item6 = itemselect.Item6,
    item7 = itemselect.Item7,
    item8 = itemselect.Item8,
}


for _, v in pairs(items) do -- loop through each item 
    v.MouseHover:Connect(function()--connect a function to the mousehover event for each item
        -- do something ......……
    end)
end

0
Thank you so much! BlauKitten 88 — 6y
Ad

Answer this question