Help With Lua Script

Sorry if this is in the wrong place its been awhile since Ive been on here, couldn’t find the noobie section. Ive been trying to get my gmod server to show the Players IP Address when they connect. I only found this bit of code on wiki.

function PrintIP( ply )
ply:PrintMessage(HUD_PRINTCONSOLE, "My IP Address: "…ply:IPAddress())
end

I made it into a Lua file and put it in the servers autorun client and server side folders. Still no luck I was hoping someone could help me out here. Seems like every time we ban a hacker we get ddosed.

[lua]
hook.Add(“PlayerConnect”,“PrintIp”,function( name, address )
print( "Player " … name … " has joined from ip " … address )
end)
[/lua]

You cannot get the players IP clientside, it must be serverside.

**[Gamemode.PlayerConnect

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerConnect)**

fucking retard


(User was permabanned for this post ("Alt of perma'd user" - Orkel))

Only the server can get the IPAddress(see wiki) of a player.

[editline]19th March 2011[/editline]

The IP is “” on the client.

[editline]19th March 2011[/editline]

Editing your post doesn’t make you any smarter.

Before I posted without thinking and put serverside instead, I edited that to clientside when I reread my post.

Is this a niggle or does Garry not want it to work correctly?

[editline]19th March 2011[/editline]

[lua]
hook.Add(“PlayerInitialSpawn”, “SaveIP”, function( p )
if p:IPAddress() != nil then
filex.Append(“ips.txt”, "Player “…p:Nick()…” has connected from the IP "…p:IPAddress())
end
end)[/lua]

Also I came up with that real quick, untested so I can’t guarantee it will work.

Awesome man I really appreciate it. I tested it and it kind of works it doesnt show the players IP when they connect to the server but i checked the data folder and it made an ip.txt file with the IPs in it. Is there anyway to make it say it in game also? It would be nice because then I would have two log files and the other one could be easier to read. Thanks again!!!

Yes, try this.

[lua]
hook.Add(“PlayerInitialSpawn”, “SaveIP”, function( p )
if p:IPAddress() != nil then
filex.Append(“ips.txt”, "Player “…p:Nick()…” has connected from the IP "…p:IPAddress())
p:PrintMessage(HUD_PRINTTALK, "Player “…p:Nick()…” has connected from the IP "…p:IPAddress())
end
end)[/lua]

I would make a new line between each user.

… "
")
to the end
filex.Append(“ips.txt”, "Player “…p:Nick()…” has connected from the IP “…p:IPAddress()…”
")

Awesome works great I appreciate the help from everyone here. I tested it out again and it only says the IP address on the screen of the person that connects, but i was thinking this is probably for the best so trolls don’t start messing with other players. Also how would you add the steam ID? It would be kick ass to have a log of IPs and steam ID’s.

p:SteamID()

[lua]
hook.Add(“PlayerInitialSpawn”, “SaveIP”, function( p )
if p:IPAddress() != nil then
filex.Append(“ips.txt”, "Player “…p:Nick()…” has connected from the IP: “…p:IPAddress()…” and with the Steam ID: “…p:SteamID()…”
")
p:PrintMessage(HUD_PRINTTALK, "Player “…p:Nick()…” has connected from the IP "…p:IPAddress())
end
end)
[/lua]

The reason it didn’t work for you is because the player was probably not made a “player entity” yet.