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

What are the exact uses of a metatable?

Asked by
raik220 20
7 years ago

I understand how to use them, but don't really see what practical use they have. It would be helpful if someone could tell me a scenario where it would be beneficial to use them.

Thank you for reading

1 answer

Log in to vote
0
Answered by
iRexBot 147
7 years ago

They're many uses for metatables. They can be used to keep data clean or help with DataStores.

Here is an example of use:


local ServerPlayers = {} game.Players.PlayerAdded:Connect(function(plr) local playerInfo = {} table.insert(playerinfo, plr.Name) if plr:IsInGroup(1) then table.insert(playerinfo, "In group") end table.insert(ServerPlayers, playerinfo) end)

What this does is creates a table called ServerPlayers. Everytime a player joins the game the server creates a table called playerInfo and putting in it the player name and if the player is in the needed group it puts "In group" in the table. Once done it inserts the table into another table. This allows you to get all of the player data in one quick use instead of grabbing 3 tables and inserting something in them.

0
Thank you! raik220 20 — 7y
0
If you want what a structure of a metatable would look like here: iRexBot 147 — 7y
0
Table = { meta = {} } iRexBot 147 — 7y
0
This is not an example of using metatables. This is what you would call "nesting" tables. Metatables are another thing entirely. One example of an important use for metatables is for Object-Oriented programming in Lua: http://wiki.roblox.com/index.php?title=Object-Oriented_Programming WillieTehWierdo200 966 — 7y
Ad

Answer this question