Hi. So i wanted to try and learn more about metatables
I have a lot of buttons. What i want to do is to change background color to black when I hover over them.
I put all the buttons in a table and I wanted to use metatable to change the backgrounds.
Instead, i get an error saying
attempt to index field 'MouseEnter' (a nil value)
Why is that? here is my code.
[Updated]'
So it doesn't error now but it still does not change background. I also noticed that the print in the metatable does not print.....
PlayGuiPrecedences[1] = PlayGui:WaitForChild("Container");
PlayGuiPrecedences[2] = PlayGuiPrecedences[1]:WaitForChild("Play");
PlayGuiPrecedences[3] = PlayGuiPrecedences[1]:WaitForChild("Settings");
PlayGuiPrecedences[4] = PlayGuiPrecedences[1]:WaitForChild("");
PlayGuiPrecedences[5] = PlayGuiPrecedences[1]:WaitForChild("MainMenu");
local ChangeBackgroundWhenHover = {
__index = function(MouseEnterTable)
MouseEnterTable.MouseEnter:Connect(function()
print("Working?");
MouseEnterTable.BackgroundColor3 = Color3New(0, 0, 0);
wait();
end);
end;
}
MouseEnterTable = setmetatable({
PlayGuiPrecedences[2];
PlayGuiPrecedences[3];
PlayGuiPrecedences[4]
}, ChangeBackgroundWhenHover)
help please. A helper in chat told me metatables in not necessary for this but i still want to know how to use metatables for situations lik this.

3 comments
but if I have a lot of objects, wouldn't i have to connect to every single one of them?
Yep. And you're going to have to anyway, if you want to connect to any of them changing.
ok thanks. I thought there would be a better way. But i guess not.