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

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

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

✓ Accepted answer

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.

3 comments

0

but if I have a lot of objects, wouldn't i have to connect to every single one of them?

0

Yep. And you're going to have to anyway, if you want to connect to any of them changing.

0

ok thanks. I thought there would be a better way. But i guess not.

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