Lua Help

I have started learning lua and have been practicing with hooks. I went and took a look at the PlayerConnect hook from: http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index0d60.html?title=Gamemode.PlayerConnect

The example was:


function GM:PlayerConnect( name, address ) 
    print( "Player " .. name .. " has joined from ip " .. address )
end 

Currently I have made this:


function PrintUniqueIDS()
    for _, v in ipairs( player.GetAll() ) do
        print( v:Nick().."'s UniqueID is: "..v:UniqueID() )
    end
end
concommand.Add("hawk_print_uids", PrintUniqueIDS)
function PrintSteamIDS()
    for _, v in ipairs( player.GetAll() ) do
        print( v:Nick().."'s SteamID is: "..v:SteamID() )
    end
end
concommand.Add("hawk_print_sids", PrintSteamIDS)
function PlayerDump(name, ip)
	chat.AddText("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
	print("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
end

hook.Add("PlayerConnect", "ConsoleLog", PlayerDump)

I kept getting an error when I tried running the lua script saying that GM was a nill value so I changed the code from this:


function GM:PlayerConnect( name, address ) 
    print( "Player " .. name .. " has joined from ip " .. address )
end 

to this:


function PlayerDump(name, ip)
	chat.AddText("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
	print("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
end

hook.Add("PlayerConnect", "ConsoleLog", PlayerDump)

This is supposed to get the name and IP address of a player when they join the game, but for some reason I went on my friends servers which had scriptnforcer disabled and I was able to run other scripts, but this one never worked. What am I doing wrong and how do I fix this and do better in my work.

P.S. I did not code everything here and I take no credit. Credit to original coders.

PlayerConnect is a serverside hook, so it won’t get called clientside.

Also, don’t use ipairs unless you need it to be in order, use pairs. Using pairs is faster but it is scrambled.

Randomly it was working on about 2 severs I went on but not on others, any idea why?

ScriptEnforcer is why

Shouldn’t work on any server unless you’re the server…

[editline]2nd February 2012[/editline]

Well the hook wont anyway, the concommands will.

I said I made sure that scriptenforcer was disabled by testing other scripts and asking the owners. Also if the hook won’t work is there still a way to make that function always work so when a person joins it shows that information?

The hook will work fine as long as you run it serverside, but chat.AddText is clientside, so it will error, you need to use usermessages to send the information to all the connected players, then use chat.AddText on the client to display it.

I want it to only be clientside, I do not want everyone to be seeing other users information.

Basically I am trying to make this:

A script that is ran on my clientside machine,
When I join a server without scriptenforcer enabled it will log the name and ip of a user that joins. This will not be running serverside so I guess I first need to get rid of the hook?

So does that mean this should work?


function PlayerDump(name, ip)
	chat.AddText("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
	print("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
end


Because then I don’t know when it would be able to grab user information because the function would not be running.
Would I have to do something like this:
(So I join a server, type hawk_start in console and then wait for people to join?)


function PlayerDump(name, ip)
	chat.AddText("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
	print("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
end

concommand.Add("hawk_start", PlayerDump)

Why do you need the player’s IP?

[editline]2nd February 2012[/editline]

Just googled his SteamID (STEAM_0:0:27284598), 2nd result is him on a SethHacker userlist…

Haven’t used Sethhack in 7 months, even talk to Seth. And it is because this is the script example from the PlayerConnect hook.

Why do you need the player’s IP though?

I don’t I copied and pasted the code from http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index0d60.html?title=Gamemode.PlayerConnect and from a script one of my friends gave me.

If I could even get help without the IP that would be fin but I do not know how to remove something without screwing it all up. Again, I am not great at lua.

what is it you want help with though, you posted like 3 times that you want to grab the player’s name/IP when they join/a half broken example asking for help making it work…

I want help creating a script that when a player joins, it will notify me in chat and console using:



chat.AddText("[Connecting player] : " .. name .. ") -- Prints to clientside chat
	print("[Connecting player] : " .. name .. ") -- Prints to console


What I don’t understand is that this works on and off.
I tried using this script:



function PrintUniqueIDS()
    for _, v in ipairs( player.GetAll() ) do
        print( v:Nick().."'s UniqueID is: "..v:UniqueID() )
    end
end
concommand.Add("hawk_print_uids", PrintUniqueIDS)
function PrintSteamIDS()
    for _, v in ipairs( player.GetAll() ) do
        print( v:Nick().."'s SteamID is: "..v:SteamID() )
    end
end
concommand.Add("hawk_print_sids", PrintSteamIDS)
function PlayerDump(name, ip)
	chat.AddText("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
	print("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
end
concommand.Add("hawk_log_start", PlayerDump)

When I type hawk_log_start I got the error “[lua est.lua:14] attempt to concatenate local ‘name’ (a userdata value)”

My lua file was in the lua folder and named test.lua
I used “lua_openscript_cl test.lua” to open the script and it would say “Running script test.lua…”
Randomly this would work and say “[Connecting Player]: name with the IP ip” and it would show the name and IP perfectly.

Overall questions:
Why does this work on and off?
Do I need concommand.Add(“hawk_log_start”, PlayerDump) or does it just always run that function after I start the script?

You aren’t hooking it up to the PlayerConnect hook, you’re trying to use a concommand which has different arguments.

I am not running it serverside and I was told the hooks are serverside. Would you mind re-coding that in a way it should actually work?

Current Code:


function PrintUniqueIDS()
    for _, v in ipairs( player.GetAll() ) do
        print( v:Nick().."'s UniqueID is: "..v:UniqueID() )
    end
end
concommand.Add("hawk_print_uids", PrintUniqueIDS)
function PrintSteamIDS()
    for _, v in ipairs( player.GetAll() ) do
        print( v:Nick().."'s SteamID is: "..v:SteamID() )
    end
end
concommand.Add("hawk_print_sids", PrintSteamIDS)
function PlayerLog(name, ip)
	chat.AddText("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
	print("[Connecting player] : " .. name .. ", with the IP '" .. ip .. "'.")
end
concommand.Add("hawk_log_start", PlayerLog)
hook.Add("PlayerConnect", "ConsoleLog", PlayerLog)

That’s wrong, every now and then an IP will sneak through Garry’s method of blocking the IP. I really don’t know why but I had that hook client side for an old cheat (before I knew what I was doing) and every now and then (super rarely) I would get a print of the players IP/SteamID/name in my console