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 button
s 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.
This isn't how you use metatables
And they're not necessary here either
Just connect the event straight to the object rather than pissing around with metatables, especially when you do not understand them.