Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Networking and hook example
- --[[
- On "getdisconnected" command used
- 1. Client->Server: Ask for the disconnected players table
- 2. Server->Client: Send the disconnected players table
- 3. Client: Print it into console
- ]]
- if SERVER then
- AddCSLuaFile()
- util.AddNetworkString("GetDCedPlayers") -- Yup, we have to use networking. This creates this string to be used for networking
- local disconnectedPlayers = {} -- Create a table for us to store the players
- -- PlayerDisconnected hook is serverside only as indicated by the blue square next to its name on the wiki
- -- Because we gather data from the server about disconnected players, the clients dont have it
- hook.Add("PlayerDisconnected", "nagasaki", function( ply )
- -- Store the disconnected player in the table by their steam id as the key
- disconnectedPlayers[ ply:SteamID() ] = ply:Nick();
- end)
- -- Networking library has send and receiving functions
- net.Receive("GetDCedPlayers", function( len, ply )
- -- In this, we receive the client's empty net message that requests the disconnected players table
- -- We need to network this table from the server to the client, its expensive to do but works
- net.Start("GetDCedPlayers")
- net.WriteTable( disconnectedPlayers )
- net.Send( ply )
- end)
- end
- if CLIENT then
- -- Clientside console command so the clients can use it
- concommand.Add("getdisconnected", function()
- net.Start("GetDCedPlayers")
- net.SendToServer()
- end)
- net.Receive("GetDCedPlayers", function( len, ply )
- local dcedPlayers = net.ReadTable() -- Now we read the server's table
- PrintTable( dcedPlayers ) -- And print it into console to be seen
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement