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

I need help understanding metatables, what are they, what do they do?

Asked by
CyzDev 26
3 years ago

Like what do they do and how do they work?

I've tried reading some posts about them but I have trouble understanding them.

Any help is appreciated.

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

the wiki definition states “bla bla rocket science 2/3=193” or something, idk.

in simple terms, they basically allow you to use player defined functions on a table.

you (the programmer) write functions and they are called when you try and use the table.

for example, say you had table A {5, 4, 9, 2} and table B {8, 1, 9, 0}

by defaulting, typing A+B into a script will cause an error. you cant add tables. how does that even work?

metatables allow you to add tables. how will they add, you ask? that is up to you. you can write it so that it adds everything in A to everything in B. you could make it so a new table is returned, with a random element from each A and B. really, you can do anything.

also, doing A[5] causes an error because A only has 4 elements. with a properly written metatable function, you could make it so calling A[5] actually does something specific.

a metatable is a table itself. you attach it to a table, using syntax.


setmetatable(x, {})

i wont add code because tbh I dont know the syntax. I am newbie developer, so I've never used them. tbh I dont even see how they’re useful. but thats basically what they do. after I had a pretty clear idea of what they were, I didnt bother trying to learn them, at least yet.

hope this answer was clear and concise :)

edit: metamethods are the methods that you actually write. doing A+B would call the metamethod that you wrote, and do whatever you wrote it to do.

if you are new to roblox scripting, or programming in general, honestly i’d say dw about it rn lol

0
Forgot to mark you as the solution, but thanks :) CyzDev 26 — 3y
Ad
Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
3 years ago

"Metatables allow tables to become more powerful than before. They are attached to data and contain values called metamethods. Metamethods are fired when a certain action is used with the datum that it is attached to."

https://developer.roblox.com/en-us/articles/Metatables

Answer this question