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

How to label multiple buttons with just one variable?

Background

Right now I have a script that labels each button such as so:

local Slot1 = BagBG.Slot1 --ImageButton
local Slot2 = BagBG.Slot2 --ImageButton
local Slot3 = BagBG.Slot3 --ImageBUtton

I'm trying to make it where it's all under one variable. So when I use the MouseEnter feature it'll apply it to each one instead of having to type out multiple MouseEnter events.

I know there's a way using tables to shorten this:

local Slots = {
		Slot1 = BagBG.Slot1,
		Slot2 = BagBG.Slot2,
 		Slot3 = BagBG.Slot3
		}
for i,v in pairs(Slots) do
	local Button = v  --is this how you go about doing it?
	Button.MouseEnter:Connect(Function
		print("It is functional")
	end)
end

However, I don't know if this is the right way. Help will be upvoted and very appreciated!

1 answer

✓ Accepted answer

You can, just some things you did are unnecessary and wrong.

local Slots = {
        Slot1 = BagBG.Slot1,
        Slot2 = BagBG.Slot2,
        Slot3 = BagBG.Slot3
        }
for _, Button in pairs(Slots) do --You can define Button here.
    Button.MouseEnter:Connect(function() --"function" needed to be lowercase, and you forgot some parenthesis.
        print("It is functional")
    end)
end

Other then that, it should work!

1 comment

0

Omg thank you so much.

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