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

Use same function for multiple buttons?

Asked by 5 years ago

So I'm working on a GUI that I want to have many buttons, some buttons that have the same function. Like a list of buttons I want the transparency changed to make it glow and play a sound. I have what it will do but I feel like there's a better way to link all the buttons up to use this hover function. What can I do to do this? This is for an inventory GUI I'm working on if that helps any.

Heres the code I've tried. Is there something I'm doing wrong or am I missing something?

local invetoryselection = screenfunctions.InvSelection
local itemselect = screenfunctions.ItemSelection
local preview = screenfunctions.Preview

local invselect = {
    weaponsselect = invetoryselection.Weapons,
    apparelselect = invetoryselection.Apparel,
    aidselect = invetoryselection.Aid,
    miscselect = invetoryselection.Misc,
    junkselect = invetoryselection.Junk
}


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,
}

function hover(n) --Attempt 2
    items[n].BackgroundTransparency = Color3.fromRGB(17, 172, 0)
    items[n].BackgroundTransparency = 0.2
end

local checkifhover = itemselect:GetChildren()

items.MouseEnter:connect(hover)

if items[1].MouseEnter == true then--Attempt 1
    items[1].BackgroundTransparency = Color3.fromRGB(17, 172, 0)
    items[1].BackgroundTransparency = 0.2
elseif items[2].MouseEnter == true then
    items[2].BackgroundTransparency = Color3.fromRGB(17, 172, 0)
    items[2].BackgroundTransparency = 0.2
elseif items[3].MouseEnter == true then
    items[3].BackgroundTransparency = Color3.fromRGB(17, 172, 0)
    items[3].BackgroundTransparency = 0.2
elseif items[4].MouseEnter == true then
    items[4].BackgroundTransparency = Color3.fromRGB(17, 172, 0)
    items[4].BackgroundTransparency = 0.2
elseif items[5].MouseEnter == true then
    items[5].BackgroundTransparency = Color3.fromRGB(17, 172, 0)
    items[5].BackgroundTransparency = 0.2
elseif items[6].MouseEnter == true then
    items[6].BackgroundTransparency = Color3.fromRGB(17, 172, 0)
    items[6].BackgroundTransparency = 0.2
elseif items[7].MouseEnter == true then
    items[7].BackgroundTransparency = Color3.fromRGB(17, 172, 0)
    items[7].BackgroundTransparency = 0.2
elseif items[8].MouseEnter == true then
    items[8].BackgroundTransparency = Color3.fromRGB(17, 172, 0)
    items[8].BackgroundTransparency = 0.2
end

Answer this question