Getting how long the player has played on the server - Gamode

Hey FacePunch

I’m making a private gamemode for my server, but has a problem i need to have a file, can’t use mysql or database, that keeps all info about players. All info would in the beginning be: time played on server, player name and player steamid. I tried to make a little code that check at first spawn if it know the player if not write new line with the info but it doesn’t write anything, what could be the problem?:


function GM:PlayerInitialSpawn( ply )
    self.BaseClass:PlayerInitialSpawn( ply )

    textdata = file.Read("TTcomRP/playedtime.txt")
    lines = string.Explode("
", textdata)
    // Now let's loop through all the lines so we can split those too
    for i, line in ipairs(lines) do
        // line is the current line, and i is the current line number
        data = string.Explode(";", line)
        playersteam = data[1]
        playername = data[2]
        playertotaltime = data[3]
    end
    if playersteam == ply:SteamID() then
    -- Nothing
    else
values = { ply:GetName(), ply:SteamID(), 0 } // create a new table with our required values

data = string.Implode(";", values)
file.AppendLine("playerdata.txt", data)
    end
end


Yah maby but i have to not only get the time connected this session but also the other sessions kind of a total time connected thing…

Try Creating the file first, seeing how it’s only a piece of small code i don’t see how it will create the file on saving.

file.AppendLine Doesn’t exist filex.Append does.

Writing to a file on think? How does that work at all.

You secret snipped :o.

It won’t work. Don’t use it.

-snip-

I know you have nice intentions etc… etc… but please, just please, for the sake of all us, please stop trying to help people, because you just can’t do it properly.
Also if you were planning to call me an idiot or anything similar, just like you usually do to people who hurt your precious ego, I suggest you just give up because it’s futile, childish and pointless.

If you don’t want to bother exploding strings, reassembling them, etc… every time, you might want to have a look at GLON. It’s a pretty handy library which turns tables into strings, and back into tables. You can read and write almost everything with it.

This way, you could have a table which holds data for every player. You load it when the server starts, and every time a new player is created, you update the table and save it. Although it might be better to create one file for every player, this way you don’t have a single file which grows indefinitely and takes insane amounts of time to update.
You have to keep in mind that writing or reading from a file is quite resource consuming, avoid doing it frequently when you don’t have to.

If you do want to create a single file per player, **[Player.UniqueID

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.UniqueID)** might be what you need. It returns a unique number which is specific to every player, use that number as your file name, and you’re good to go. This way, if a file doesn’t exist, it means that the corresponding player hasn’t been registered yet.
Then, update the file when the player disconnects. Or every 5 minutes, just in case.

For the total time connected, make it start at 0, and every time the player disconnects, add **[Player.TimeConnected

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.TimeConnected)** to it, and save.
The total time connected in real-time is “saved total time connected + time connected on current session”.

Hope that helped.