What are they? I’ve never touched on them but they seem like an important aspect of GMod Lua, where do I start?
Look here. I think Deco explains it very well.
If you want to learn how to use metatable to make functions for your players, look at this:
local meta = FindMetaTable( “Player” ) – Grabs player object for us to make functions with
if (!meta) then return end --if not a valid metatable then do nothing
function meta:SetTotalScore( num ) --This is a made up function. The first argument is a number, but you could call it anything you want. You could call it “lol” if you wanted.
self:SetNWInt( “TScore”, num ) --Make up an original Networked Integer if you are setting a number.
function meta:GetTotalScore() --This retrieves that Networked Integer
return self:GetNWInt( “TScore” ) --This returns the NWInt “TScore”, therefore this function is basically equal to whatever you set in SetTotalScore.
function meta:SetMyWords( words ) --Lets make another function. As you can see, the args can be called whatever you want.
self:SetNWString( “MadeUpWords”, words ) --This one is a string this time, so make up an original Networked String.
function meta:GetMyWords() --This retrieves that Networked String
return self:GetNWString( “MadeUpWords” ) --See?
function SetSomeStuffUp(ply) --Here is an example function called somewhere else.
ply:SetTotalScore(9001) --Set the player’s total score. As you can see, 9001 is the “num” argument shown earlier.
ply:SetMyWords(“Metatables!”) --Set the player’s total score. As you can see
print(ply:GetTotalScore()) --this should print a number that’s OVER 9000!!!
function LetsDoStuff(ply) --Here is an example function called somewhere else.
print(“What’s your favorite meme?”)
if ply:GetTotalScore() > 9000 then
print(“It’s over 9000!”)
print("IDK, my score is only "…ply:GetTotalScore())
print(“What do you understand?”)
/Console should print:
“What’s your favorite meme?”
“It’s over 9000!”
“What do you understand?”