Advertisement
Exho

Untitled

Aug 6th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. -- Networking and hook example
  2. --[[
  3.     On "getdisconnected" command used
  4.     1. Client->Server: Ask for the disconnected players table
  5.     2. Server->Client: Send the disconnected players table
  6.     3. Client: Print it into console
  7.  
  8. ]]
  9.  
  10. if SERVER then
  11.     AddCSLuaFile()
  12.     util.AddNetworkString("GetDCedPlayers") -- Yup, we have to use networking. This creates this string to be used for networking
  13.    
  14.     local disconnectedPlayers = {} -- Create a table for us to store the players
  15.  
  16.     -- PlayerDisconnected hook is serverside only as indicated by the blue square next to its name on the wiki
  17.     -- Because we gather data from the server about disconnected players, the clients dont have it
  18.     hook.Add("PlayerDisconnected", "nagasaki", function( ply ) 
  19.         -- Store the disconnected player in the table by their steam id as the key
  20.         disconnectedPlayers[ ply:SteamID() ] = ply:Nick();
  21.     end)
  22.    
  23.     -- Networking library has send and receiving functions
  24.     net.Receive("GetDCedPlayers", function( len, ply )
  25.         -- In this, we receive the client's empty net message that requests the disconnected players table
  26.         -- We need to network this table from the server to the client, its expensive to do but works
  27.        
  28.         net.Start("GetDCedPlayers")
  29.             net.WriteTable( disconnectedPlayers )
  30.         net.Send( ply )
  31.     end)
  32. end
  33.  
  34. if CLIENT then
  35.    
  36.     -- Clientside console command so the clients can use it
  37.     concommand.Add("getdisconnected", function()
  38.         net.Start("GetDCedPlayers")
  39.         net.SendToServer()
  40.     end)
  41.    
  42.     net.Receive("GetDCedPlayers", function( len, ply )
  43.         local dcedPlayers = net.ReadTable() -- Now we read the server's table
  44.        
  45.         PrintTable( dcedPlayers ) -- And print it into console to be seen
  46.     end)
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement