Lua hates tables

So I’ve started my own gamemode, going good so far even for my first try.
… but as expected, there’s a problem with something.

I tried to make my stats save to the file, there was problem with filename which can’t have :, and since I made filename be same as the steamID so I had to use string.gsub to change all : in local PlayerSteamID to _, but that’s not the problem.

The problem is, I tried to make tables save in this file, so it’d be easy to edit, easy to read and generaly easy to use, but all I get is this:

Hook ‘PlayerSaveStats’ Failed: [@gamemodes\black_rose\gamemode\init.lua:70] bad argument #2 to ‘Write’ (string expected, got table)

I know, it’s not working and obviously because of the fact that it’s the table, so I would like to know how to use it in another way, using both tables and file.Write.

There’s my code:

//Save stats
function SaveStats( ply )
local PlayerID = ply:SteamID()
local PlayerIDfile = string.gsub(PlayerID,":","_")

local PlayerStatsTbl = {}
PlayerStatsTbl["hp"] = 100
PlayerStatsTbl["str"] = 2
PlayerStatsTbl["agi"] = 2
PlayerStatsTbl["lvl"] = 1
PlayerStatsTbl["exp"] = 0
PlayerStatsTbl["stats"] = 0

if not file.Exists("data/accounts/" ..PlayerIDfile.. ".txt") then
	file.Write("data/accounts/" .. PlayerIDfile .. ".txt", PlayerStatsTbl)

	local PlayerStats = file.Read("data/accounts/" .. PlayerIDfile .. ".txt")
hook.Add( "PlayerDeath", "PlayerSaveStats", SaveStats )

If you want the saved files to be human readable (and editable) then you can use **[G.KeyValuesToTable](** and **[G.TableToKeyValues](**.
If not, you should go with **[Glon.encode](** and **[Glon.decode](**.

[editline]7th January 2011[/editline]

Also, your title sucks.

It’s because you can’t write tables to files. Use glon.encode (Table) and glon.decode (Table) to convert tables to strings and back, and write them to file.

P.S. And better use SQL, it’s much faster in use.

EDIT: Whoops, wasn’t that quick.

There, both have lua helper <3

Now I got something like that:
function arguments expected near ‘local’

While reading

 function GM:PlayerSpawn(ply)

local PlayerID = ply:SteamID
local PlayerIDfile = string.gsub(PlayerID,":","_")

	StatsLoad = file.Read "data/accounts/" ..PlayerIDfile.. ".txt")
	CurrentStats = glon.decode(StatsLoad)



You forgot to put a left parenthesis on your 4th line.

StatsLoad = file.Read **(**"data/accounts/" ..PlayerIDfile.. ".txt")

Guess it’s that.

Wait, misunderstood what the error was. Functions that don’t take arguments still require parenthesis. So it should be ply:SteamID () instead of ply:SteamID

Changed it, but it’s still the same problem
[gamemodes\black_rose\gamemode\init.lua:27] function arguments expected near ‘local’


Again, another thingy: [gamemodes\black_rose\gamemode\init.lua:32] attempt to index local ‘CurrentStats’ (a nil value)(Hook: PlayerSpawn)

Friend told me something about GetNWint, but I don’t get what he meant

That may be because the StatsLoad value is nil, meaning it wasn’t assigned anything. Look into the roots of the problem, this may be caused by a non-existing file, or the file does exist but has no data in it (Decoding a nil…).