• Getting how long the player has played on the server - Gamode
    7 replies, posted
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?: [code]function GM:PlayerInitialSpawn( ply ) self.BaseClass:PlayerInitialSpawn( ply ) textdata = file.Read("TTcomRP/playedtime.txt") lines = string.Explode("\n", 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 [/code]
[url]http://wiki.garrysmod.com/?title=Player.TimeConnected[/url]
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.
[QUOTE=Cubar;25002956][lua] hook.Add("Initialize","Initilizestuff", function() if not file.IsDir("TTcomRP") then file.CreateDir("TTcomRP") end end) hook.Add("PlayerInitialSpawn","PlayerConnectedtime", function(ply) if not file.Exists("TTcomRP/"..ply:UniqueID().."playedtime.txt") then file.Write("TTcomRP/"..ply:UniqueID().."playedtime.txt","0") end if file.Exists("TTcomRP/"..ply:UniqueID().."playedtime.txt") then ply.StartTime = file.Read("TTcomRP/"..ply:UniqueID().."playedtime.txt") elseif not file.Exists("TTcomRP/"..ply:UniqueID().."playedtime.txt") then ply.StartTime = CurTime() end end) hook.Add("Think","time", function() if file.Exists("TTcomRP/"..ply:UniqueID().."playedtime.txt") then for k,ply in pairs(player.GetAll()) do GetTimeConnected2(ply) file.Write("TTcomRP/"..ply:UniqueID().."playedtime.txt",GetTimeConnected2(ply)) end end end) function GetTimeConnected2(ply) return math.Round(CurTime() - ply.StartTime) end function SetTimeConnected2(ply,num) return math.Round(GetTimeConnected2(ply) + num) end [/lua] [/QUOTE] It won't work. Don't use it.
-snip-
[QUOTE=Cubar;25003089]Lol it was using 100% cpu that's why i removed it, it did save, oh gawd idiot above me, it was originally on timer.Create.[/QUOTE] 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 [url=http://wiki.garrysmod.com/?title=Glon]GLON[/url]. 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, [b][url=http://wiki.garrysmod.com/?title=Player.UniqueID]Player.UniqueID [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] 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 [b][url=http://wiki.garrysmod.com/?title=Player.TimeConnected]Player.TimeConnected [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] to it, and save. The total time connected in real-time is "saved total time connected + time connected on current session". Hope that helped.
Sorry, you need to Log In to post a reply to this thread.