• GLua question.
    26 replies, posted
I am writing a script to get a table of currently connected players and their steamids, and paste them into a file. However, I need a 30-second interval so it will auto update. I don't know a function or a way to do this. however if you do know a function that will fire when a player connects that would be better. So will both c:
See this [url]http://wiki.garrysmod.com/page/GM/PlayerConnect[/url] for a hook that calls when players connect. If you want infinite auto-updating every 30 seconds look at [url]http://wiki.garrysmod.com/page/timer/Create[/url] with 0 as an argument for repititions, but the player connect should suffice.
Here's what I have. [CODE] function GM:PlayerConnect( name, ip ) for _, v in pairs( player.GetAll() ) do file.Append( "userlist.txt", v:Nick() .. ", " .. v:SteamID() .. "\n") print("Appended the userlist.txt in the server's data file.") end end [/CODE] And I get an error. [ERROR] lua/autorun/luatest.lua:20: atttempt to index global GM (a nil value) 1. unknown - lua/autorun/luatest.lua:20 Just so you know I barely got the hang of GLua.
Just put that in a hook... [CODE] hook.Add( 'PlayerConnect', "SOME_UNIQUE_NAME", function( name, ip ) for _, v in pairs( player.GetAll() ) do file.Append( "userlist.txt", v:Nick() .. ", " .. v:SteamID() .. "\n") print("Appended the userlist.txt in the server's data file.") end end ) [/CODE] And see if it works then.
How so? Again i just started :p
[QUOTE=ZombieKingAAA;49670070]How so?[/QUOTE] I updated my post Not sure if that'd work, though... you can test it I suppose Also, if you're doing file.Append to add all that stuff to the file, try clearing the file's contents first before doing that so it doesn't make a massive repeated list
Im testing around.
How are you running this exactly? The hook library definitely exists in GMod, yet the error seems to think it's an undefined var. [B]EDIT: [/B]That's odd, the hook library exists clientside as well... don't know why the error would only happen clientside...
Here is the full code: [CODE] if(SERVER) then file.CreateDir( "Test" ) file.Write( "userlist.txt", "#//This is for the loading screen. \n #//Username, SteamID" ) end if(SERVER) then concommand.Add( "append_player_info", function() for _, v in pairs( player.GetAll() ) do file.Append( "userlist.txt", v:Nick() .. ", " .. v:SteamID() .. "\n") end print("Appended the userlist.txt in the server's data file.") end ) end hook.Add( 'PlayerConnect', "SOME_UNIQUE_NAME", function( name, ip ) for _, v in pairs( player.GetAll() ) do file.Append( "userlist.txt", v:Nick() .. ", " .. v:SteamID() .. "\n") print("Appended the userlist.txt in the server's data file.") end end ) [/CODE] I also edited my post, im messing around for a sec. And i'm doing this all serverside.
What file path is this in? I don't see the problem...
Nevermind. I fixed it. Thank you so much for the help, I feel bad for putting you through that XD Seriously thank you. Now im going to work on it more.
[QUOTE=ZombieKingAAA;49670089]It is in autorun. C:\gmodserver\garrysmod\lua\autorun\luatest.lua [editline]4th February 2016[/editline] So, the code i presented is the code im using. I will save the code, and no errors appear. Again, this is in C:\gmodserver\garrysmod\lua\autorun, and serverside. When i would spawn a bot, it did not append the file.[/QUOTE] That's cause PlayerConnect only works with players, not bots (AFAIK)
If your loading screen is on a external web server, you should look into using an RCON protocol to get the players. [url]https://github.com/xPaw/PHP-Source-Query[/url] Seems way more effective then saving a file every 5 minutes
[QUOTE=MPan1;49670097]That's cause PlayerConnect only works with players, not bots[/QUOTE] I edited my code, and things worked. [editline]4th February 2016[/editline] Now, when a player disconnects, I need the file to clear itself and update itself. Should I use the code here [CODE]hook.Add( 'PlayerDisconnected', "SOME_UNIQUE_NAMETWO", function( name, ip ) for _, v in pairs( player.GetAll() ) do file.Write( "userlist.txt", v:Nick() .. ", " .. v:SteamID() .. "\n") print("Appended the userlist.txt in the server's data file.") end end )[/CODE]?
[QUOTE=ZombieKingAAA;49670101]I edited my code, and things worked. [editline]4th February 2016[/editline] Now, when a player disconnects, I need the file to clear itself and update itself. Should I use the code here [CODE]hook.Add( 'PlayerDisconnected', "SOME_UNIQUE_NAMETWO", function( name, ip ) for _, v in pairs( player.GetAll() ) do file.Write( "userlist.txt", v:Nick() .. ", " .. v:SteamID() .. "\n") print("Appended the userlist.txt in the server's data file.") end end )[/CODE]?[/QUOTE] no, you need to find the line where you saved the information of the player and remove it, I recommend using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/Split]string.Split[/url] with "\n" as separator and loop through the results.
[QUOTE=GGG KILLER;49670157]no, you need to find the line where you saved the information of the player and remove it, I recommend using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/Split]string.Split[/url] with "\n" as separator and loop through the results.[/QUOTE] Im not sure how.
Try this. [code] if SERVER then local function WriteUserlist() if not file.Exists("Test", "DATA") then file.CreateDir("Test") end -- check if the folder exists, if not create it. local buffer = "#//This is for the loading screen. \n #//Username, SteamID"; for _, v in pairs( player.GetAll() ) do buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() end file.Write( "Test/userlist.txt", buffer ) -- we are recreating the file every time this function runs, its easier print("Appended the userlist.txt in the server's data file.") -- remove this if you want end concommand.Add( "append_player_info", WriteUserlist ) hook.Add( "PlayerConnect", "UserListUpdate", WriteUserlist) -- Rewrite the list whenever a player connects or disconnects hook.Add("PlayerDisconnected", "UserListUpdate", WriteUserlist) --hook.Add("PlayerInitialSpawn", "UserListUpdate", WriteUserlist) --you might want to use this instead of PlayerConnect if you only want to see players actually in game end [/code] [B]Edit:[/B] Updated.
[QUOTE=Ducky3426;49670251]Try this. [code] if SERVER then local function WriteUserlist() if not file.Exists("Test", "DATA") then file.CreateDir("Test") end -- check if the folder exists, if not create it. local buffer = "#//This is for the loading screen. \n #//Username, SteamID"; for _, v in pairs( player.GetAll() ) do buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID()) end file.Write( "Test/userlist.txt", buffer ) -- we are recreating the file every time this function runs, its easier print("Appended the userlist.txt in the server's data file.") -- remove this if you want end concommand.Add( "append_player_info", WriteUserlist ) hook.Add( "PlayerConnect", "UserListUpdate", WriteUserlist) -- Rewrite the list whenever a player connects or disconnects hook.Add("PlayerDisconnected", "UserListUpdate", WriteUserlist) --hook.Add("PlayerInitialSpawn", "UserListUpdate", WriteUserlist) --you might want to use this instead of PlayerConnect if you only want to see players actually in game end [/code][/QUOTE] I got an error. [IMG]http://i.imgur.com/URcFuTS.png[/IMG]
[QUOTE=ZombieKingAAA;49670341]I got an error. [IMG]http://i.imgur.com/URcFuTS.png[/IMG][/QUOTE] [CODE]buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID()[/CODE]
[QUOTE=Ducky3426;49670251]Try this. [code] if SERVER then local function WriteUserlist() if not file.Exists("Test", "DATA") then file.CreateDir("Test") end -- check if the folder exists, if not create it. local buffer = "#//This is for the loading screen. \n #//Username, SteamID"; for _, v in pairs( player.GetAll() ) do buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() end file.Write( "Test/userlist.txt", buffer ) -- we are recreating the file every time this function runs, its easier print("Appended the userlist.txt in the server's data file.") -- remove this if you want end concommand.Add( "append_player_info", WriteUserlist ) hook.Add( "PlayerConnect", "UserListUpdate", WriteUserlist) -- Rewrite the list whenever a player connects or disconnects hook.Add("PlayerDisconnected", "UserListUpdate", WriteUserlist) --hook.Add("PlayerInitialSpawn", "UserListUpdate", WriteUserlist) --you might want to use this instead of PlayerConnect if you only want to see players actually in game end [/code] [B]Edit:[/B] Updated.[/QUOTE] It does not create the folder, but I can make a work around for it. When it is suppost to "update" the file it says: ""#//This is for the loading screen. \n #//Username, SteamID""
If I remember correcly, [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/file/Exists]file.Exists[/url] can be expensive in certain situations, and it's better to just call [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/file/CreateDir]file.CreateDir[/url] without the if statement because it's quite cheap.
[QUOTE=James xX;49670428]If I remember correcly, [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/file/Exists]file.Exists[/url] can be expensive in certain situations, and it's better to just call [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/file/CreateDir]file.CreateDir[/url] without the if statement because it's quite cheap.[/QUOTE] I fixed that ty [editline]4th February 2016[/editline] However, I still have an issue. When a player joins, the file just writes "#//This is for the loading screen. #//Username, SteamID": [editline]4th February 2016[/editline] Then When a player left... [IMG]http://i.imgur.com/g9xkrIA.png[/IMG]
replace [lua] buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() [/lua] for [lua] if v and v.IsValid and v:IsValid() then buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() end [/lua]
[QUOTE=GGG KILLER;49670597]replace [lua] buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() [/lua] for [lua] if v and v.IsValid and v:IsValid() then buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() end [/lua][/QUOTE] or you could just do [lua] if IsValid( v ) then buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() end [/lua] Why check 3 things when you could just check 1.
[QUOTE=James xX;49670620]or you could just do [lua] if IsValid( v ) then buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() end [/lua] Why check 3 things when you could just check 1.[/QUOTE] According to the [url=http://wiki.garrysmod.com/page/Global/IsValid]Wiki[/url]: [quote]Checks that an object is not nil, has an IsValid method and if this method returns true.[/quote] Or if you want better reference, the [url=https://github.com/garrynewman/garrysmod/blob/f0880b84b9dcae406e92ea2277120005524602df/garrysmod/lua/includes/util.lua#L166-L176]code[/url]: [lua] --[[--------------------------------------------------------- Returns true if object is valid (is not nil and IsValid) -----------------------------------------------------------]] function IsValid( object ) if ( !object ) then return false end if ( !object.IsValid ) then return false end return object:IsValid() end [/lua] Its the same thing I did on that if but in an external function.
Nope, didn't work. Here's my code. [CODE]if SERVER then file.CreateDir("Test") file.Write( "Test/userlist.txt", buffer ) local function WriteUserlist() if not file.Exists("Test", "DATA") then file.CreateDir("Test") end -- check if the folder exists, if not create it. if v and v.IsValid and v:IsValid() then buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() end for _, v in pairs( player.GetAll() ) do buffer = buffer .. "\n" .. v:Nick() .. ", " .. v:SteamID() end file.Write( "Test/userlist.txt", buffer ) -- we are recreating the file every time this function runs, its easier print("Appended the userlist.txt in the server's data file.") -- remove this if you want end concommand.Add( "append_player_info", WriteUserlist ) hook.Add( "PlayerConnect", "UserListUpdate", WriteUserlist) -- Rewrite the list whenever a player connects or disconnects hook.Add("PlayerDisconnected", "UserListUpdate", WriteUserlist) --hook.Add("PlayerInitialSpawn", "UserListUpdate", WriteUserlist) --you might want to use this instead of PlayerConnect if you only want to see players actually in game end[/CODE] Nothing happens when a player joins and an error happens when one leaves.
You didn't pass the function the variable of the player, there for 'v' is nil. And when you get an error, you should probably write it here, instead of saying: "I GOT AN ERROR!".
Sorry, you need to Log In to post a reply to this thread.