How do I append rows to a table?

Say I wanted to make a table of every player in the server, and add certain players to the table with comments added by an admin. I want to send the playerdata, but I also to add the string text along with each player so I can print it in a players console, like for a warning system. How would I do that? I looked around for adding rows to lua tables and it was all ridiculously high-level lua code that I couldn’t begin to understand. Can you guys help me out?

If your table only has numeric indices (and no empty spaces), you can get the length of the table and add a new item there.
Lets say your table is called tbl.

#tbl is the length of your table.
To add an item to the end of it:

tbl[#tbl+1] = "the appended row"

You can also do
[lua]table.insert(tbl, “the appended row”)[/lua]
I prefer writing it the first way.

If you are not using numeric indices, then there is no real ‘order’ to the rows and you just add it to the table
[lua]tbl[‘myNewKey’] = ‘my new value’[/lua]

No I don’t want to add a new key/value pair to an already existing table. I basically want to be able to add a string to be stored corresponding to each player, but accessible easily. Is there a way to do that? figured the easiest way to do that is to use tables but there’s gotta be an easier way. I just can’t find it.

PLAYER = FindMetaTable( “Player” );

function PLAYER:AddComment( _comment )

table.Insert( self.comments, _comment );


function PLAYER:ShowComments()

PrintTable( self.comments );


Then you can do like

ply:AddComment( “blabla” );

will show
1 = “blabla”

Thank you! This works for exactly what I need :slight_smile:

May I ask though, why does this work? what exactly are you doing in the code that causes those values to “carry over”?

I’m not sure what you mean by carry over.
PLAYER is the meta table “player”, which contains all functions such as SetHealth, ply:SetHealth( 100 );, inside the code it sets the self object health, which is the ply object, so each player will have this function available and it will only modify that player object.

So basically it appends this value to each players playerdata?

the function SetComment above, inserts a row into the “self.comments”, which is a variable defined inside the player object.

comments = {};

self.comments, means comments inside the “self” object which in this case is a player, it contains alot of other variables and functions, and we made two new functions to add and show data from a table, that table is inside each player object, so depending on which player object we call the function in, that player object will have a new row inside its comments table

Ah, makes sense. Does every entity have a “self” object?

Every entity is an object.
An example:

self:SetModel( “model.mdl” );


ENT:Use(), we set what happens to this entity when used.
self:SetModel, now we’re inside the function - so the ENT is self.

self:SetModel( “model.mdl” );

This entities initialize function will set the entities model to model.mdl

Now a lot of the SWEPs I was looking at make a lot more sense. Thank you :slight_smile:

Basically, when you call a method with : instead of ., it will add a local self to that method.

function ENT:Loco()

function ENT:Initialize()
self.Loco() -- Only a period

If you now run the code, it will print “Entity [sometrhing]” and next line it will print “nil”.