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

Did I use 'table.Insert' correctly?

Asked by
IcyEvil 260 Moderation Voter
10 years ago

Here is the coding

function admin()
                   if msg:sub(1,6) == "admin " and isAdmin(spkr) then
                        vic = msg:sub(1,9)
                        if findPlayer(vic) then
                            table.Insert(vic, #admins)
                    end

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

All of the built-in Lua functions are lowercase. It's table.insert, not table.Insert.


The parameters to table.insert are (whichTable, whereInTable, whatThingToPutIn) (and the whereInTable is optional -- it defaults to the end of the table).


To add vic to the table admins, we would do

table.insert(admins, vic)

EDIT: I screwed up the order of whereInTable and whatThingToPutIn, thank you Perci1 for reminding me. (whereInTable is optional and so this mistake doesn't affect the actual code snippet here)

0
Thanks, I will Mark this in case I forget, Thanks :D IcyEvil 260 — 10y
0
Is it not table.insert(whichTable, where, whatThingToPutIn)? Where where is optional of course. jakedies 315 — 10y
1
Can't you also do table:insert(whatThingToPutIn,where)? Perci1 4988 — 10y
0
Perci1: No. `a:b(c)` is sugar for `a.b(a,c)`, which would mean all tables would require a `insert` property for `t:insert` to work; that would defy the assumption that `{}` has no values in it anywhere. BlueTaslem 18071 — 10y
View all comments (2 more)
0
But you can do stringname:sub() instead of string.sub(), so why should it be different for this? Perci1 4988 — 10y
0
Try it out! Strings have those things built in, so it works for them -- they also don't actually have properties, it's just magic for them (("cat").sub will error) BlueTaslem 18071 — 10y
Ad

Answer this question