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

What is this Metatable error? I want to change the background when I hover.

Asked by 6 years ago
Edited 6 years ago

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.

1 answer

Log in to vote
1
Answered by 6 years ago

Spoiler Alert

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.

0
but if I have a lot of objects, wouldn't i have to connect to every single one of them? LightYagamiTheKira 116 — 6y
0
Yep. And you're going to have to anyway, if you want to connect to any of them changing. User#6546 35 — 6y
0
ok thanks. I thought there would be a better way. But i guess not. LightYagamiTheKira 116 — 6y
Ad

Answer this question