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
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
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.
This discussion is preserved as it was. New answers, comments, and votes are closed.
4 comments
Thank you!
If you want what a structure of a metatable would look like here:
Table = { meta = {} }
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