GLua question.

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 http://wiki.garrysmod.com/page/GM/PlayerConnect for a hook that calls when players connect.

If you want infinite auto-updating every 30 seconds look at http://wiki.garrysmod.com/page/timer/Create with 0 as an argument for repititions, but the player connect should suffice.

Here’s what I have.



function GM:PlayerConnect( name, ip )
for _, v in pairs( player.GetAll() ) do
		file.Append( "userlist.txt", v:Nick() .. ", " .. v:SteamID() .. "
")
		print("Appended the userlist.txt in the server's data file.")
end
end


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…



hook.Add( 'PlayerConnect', "SOME_UNIQUE_NAME", function( name, ip )
	for _, v in pairs( player.GetAll() ) do
		file.Append( "userlist.txt", v:Nick() .. ", " .. v:SteamID() .. "
")
		print("Appended the userlist.txt in the server's data file.")
	end
end )


And see if it works then.

How so?

Again i just started :stuck_out_tongue:

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.

**EDIT: **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:



if(SERVER) then

file.CreateDir( "Test" )
file.Write( "userlist.txt", "#//This is for the loading screen. 
 #//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() .. "
")
	
	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() .. "
")
		print("Appended the userlist.txt in the server's data file.")
	end
end )

	

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.

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. https://github.com/xPaw/PHP-Source-Query Seems way more effective then saving a file every 5 minutes

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


hook.Add( 'PlayerDisconnected', "SOME_UNIQUE_NAMETWO", function( name, ip )
	for _, v in pairs( player.GetAll() ) do
		file.Write( "userlist.txt", v:Nick() .. ", " .. v:SteamID() .. "
")
		print("Appended the userlist.txt in the server's data file.")
	end
end )

?

no, you need to find the line where you saved the information of the player and remove it, I recommend using

string.Split with "
" as separator and loop through the results.

Im not sure how.

Try this.



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. 
 #//Username, SteamID";
		for _, v in pairs( player.GetAll() ) do
			buffer = buffer .. "
" .. 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



Edit:
Updated.

I got an error.


buffer = buffer .. "
" .. v:Nick() .. ", " .. v:SteamID()

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.
#//Username, SteamID""