Accessing tables

So in shared.lua I have a table, like this

[lua]comclasses = {}

comclasses[scout] =
{
“models/player/combine_soldier.mdl”,
500,
{
“weapon_pistol”,
“weapon_shotgun”,
“weapon_crowbar”
}
}

comclasses[soldier] =
{
“models/player/combine_soldier_prisonguard.mdl”,
200,
{
“weapon_rpg”,
“weapon_shotgun”,
“weapon_stunstick”
}
}

comclasses[pyro] =
{
“models/player/combine_super_soldier.mdl”,
350,
{
“weapon_bugbait”,
“weapon_shotgun”,
“weapon_crowbar”
}
}[/lua]

the first value of the table is the model of that class’ players, the second is the speed, the third is the weapons.

to access the classes data, i wrote this in itit.lua.

[lua]function GM:PlayerSpawn(ply)
self.BaseClass:PlayerSpawn( ply )

if ply:Team() == 1 then
	local speed = comclasses[ply.Class][1]
	ply:SetWalkSpeed(speed)
	ply:SetRunSpeed(speed+200)
end

end[/lua]

let’s say ply.Class is “scout”… it’s a string. So I wanna access the speed, which is the second value in comclasses[scout]. Why when i write [lua]local speed = comclasses[ply.Class][1][/lua] I get the error



ERROR: GAMEMODE:'PlayerSpawn' Failed: HLTF/gamemode/init.lua:15: attempt to index field '?' (a nil value)


I don’t really get whats the problem, and i would be thankful if you explain me how to access the class data i stored in that table correctly

comclasses[“scout”]
comclasses[“pyro”]
comclasses[“soldier”]

And so on.

I knew that, but when I do comclasses[“scout”] I get a table, right? (Referring to the code) it should be

[lua]{
“models/player/combine_soldier.mdl”,
500,
{
“weapon_pistol”,
“weapon_shotgun”,
“weapon_crowbar”
}
}[/lua]

this table contains 3 items: a string, which is the model, a number, which is the speed, and another new table which contains 3 strings, for the weapons. Let’s say I want to fill a variable with the speed number, in this case 500. I tried to write
[lua]local speed = comclasses[scout][1][/lua] but it seems not to work, because I get


ERROR: GAMEMODE:'PlayerSpawn' Failed: HLTF/gamemode/init.lua:15: attempt to index field '?' (a nil value)

So what’s the right way of accessing the speed number, and the other variables aswell?

he means when naming ur tables… comclasses[“scout”] = { “models/erew/fads/das/a” etc etc }
not when calling them.

Basically the indexes need quotations to make them strings.

It looks like your errors are coming from improper syntax. Since you are using bracket syntax for indexing your tables, if the index is not numerical, you need quotation marks to access it, because it is being indexed via a string.

You are doing this:

[lua]local myvariable = mytable[myindex][/lua]

When you should be doing this:

[lua]local myvariable = mytable[“myindex”][/lua]

When you are accessing the index through numbers, however, you do not need the quotes. So this is fine:

[lua]local myvariable = mytable[1][/lua]

Without the quotes it reads scout, pyro, and soldier as variables, which have not been initialized.

Thanks guys! I thought those would work even without the "