Question about sending tables between server and client.

First off, here is my code;

Server:



util.AddNetworkString( "SendUserCreateParty" )

net.Receive( "SendUserCreateParty", function( len, ply )

	local PartyName = net.ReadString()
	local PartyDesc = net.ReadString()

	if !IsValid( ply, len ) or 
	   !isAlphanumeric( PartyName, PartyDesc )
	then
		ply:ChatPrint( "Party name may only contain A-Z" )
		return 
	end
	
	ply:ChatPrint( "Party Name: " .. PartyName .. "; Party Desc: " .. PartyDesc )

end )

--[Create party func]
local parties = {}

function plyMeta:createParty( ply, partyName, partyDesc )

	local plyID = ply:AccountID()

	parties[plyID] = {
		name = partyName,
		desc = partyDesc,
		members = {

		}
	}

end


Shared:



local plyMeta = FindMetaTable( "Player" )

function plyMeta:getParty()

	local plyID = self:AccountID()

	if parties[plyID] then
		return "true"
	end

end


Client:


Simply using ply:GetParty().name

It throws this error:



[ERROR] addons/darkrpmodification-master/lua/darkrp_modules/party/sh_party.lua:10: attempt to index global 'parties' (a nil value)
  1. getParty - addons/darkrpmodification-master/lua/darkrp_modules/party/sh_party.lua:10
   2. func - addons/darkrpmodification-master/lua/darkrp_modules/party/cl_party.lua:99
    3. unknown - lua/includes/extensions/net.lua:32


I know why this is happening; It’s because the client doesn’t have access to the parties table that was created server-side. What’s the best way to get the parties table to the shared file so the client can access info from it? Should I use net messages? If so, how would I set it up? (I’m not asking how to use the netlibrary but more so how to apply it to my situation). Thanks for reading and sorry for the mega-post.

I will elaborate more if needed.

[editline]19th January 2017[/editline]

I tried making the parties table global, and doing this:



function plyMeta:getPartyName()

	return parties[self:AccountID()].name

end


But I realize it only allows the server instance of the shared file to access it, not the client instance.

When the client connects, you want to send them a net message containing party info. Here’s how I would structure that net message:

  • Number of (active) parties as an UInt
  • for every (active) party in the parties table, the name and desc as a string

In code, it would look like this:

[lua]
net.Start(“SyncPartyList”)
net.WriteUInt(table.Count(parties), 32)
for id, party in pairs (parties) do
net.WriteString(party.name)
net.WriteString(party.desc)
end
net.Send(ply)
[/lua]

To be honest, sending the whole party list to the client once they connect is inefficient and you should only send the strict minimum, when needed.
What I usually do is that if the client sees a player whose party info isn’t found, a console command/net message should be ran/sent to server to request that particular player’s party info. That way, you avoid sending a (potentially) big chunk of data every time a player joins and it saves you the trouble of having to send party data in blocks if you happen to exceed the data limit.