Network String Alternative

Is there a way to retrieve a serverside variable from clientside, such as tables. Right now i’m using GetNWString() then explode the string.



local string = ""
for k,v in pairs(table) do

string = string ..";".. v

end

self:SetNWString("Inv", string)



then on clientside I do something like




local inv = string.Explode(";",self:GetNWString("Inv",""))



I’m more than sure that there is a more efficient way of doing this. All help is appreciated

Maybe this:

net.WriteTable

net.ReadTable

by the looks of it you’re making an inventory?




local Meta = FindMetaTable("Player");

util.AddNetworkString("Inventory:Update")


hook.Add("PlayerInitialSpawn","INV:IN",function( self )

	self.Inventory = {}

end

function Meta:AddItem( Name, Amount )

	assert( isstring( Name ) && isnumber( Amount ), "AddItem Format: Item Name( String ), Amount( Int )" )
	
	if not self.Inventory[Name] then self.Inventory[Name] = 0 end
	self.Inventory[Name] = self.Inventory[Name] + Amount
	
end

function Meta:TakeItem( Name, Amount )

	assert( isstring( Name ) && isnumber( Amount ), "AddItem Format: Item Name( String ), Amount( Int )" )
	
	if not self.Inventory[Name] then self.Inventory[Name] = 0 end
	self.Inventory[Name] = self.Inventory[Name] - Amount

end

function Meta:HasItem( Name, Amount )

	assert( isstring( Name ) && isnumber( Amount ), "AddItem Format: Item Name( String ), Amount( Int )" )	
	return self.Inventory[Name] - Amount >= 0
	
end

-- This one sends a single item, this is good to use when taking/adding one item, there is no need to re-network the entire inventory.
function Meta:UpdateItem( Name, Amount )

	assert( isstring( Name ) && isnumber( Amount ), "AddItem Format: Item Name( String ), Amount( Int )" )	
	
	net.Start("Inventory:Update")
		net.WriteString( Name )
		net.WriteUInt( Amount, 16 )
	net.Send( self )


end

-- This one will send the entire inventory ( use this when a player joins )

function Meta:SendInventory()

	for k,v in pairs( self.Inventory ) do
	
		net.Start("Inventory:Update")
		net.WriteString( k )
		net.WriteUInt( v, 16 )
		net.Send( self )
	
	end

end

// -- Client Side Code -- //

hook.Add("Initialize","Inventory:IN", function()
    Inventory = {}
end)

net.Receive("Inventory:Update", function( len, ply )

	if not( Inventory ) then Inventory = {} end
	local Name, Amount = net.ReadString(), net.ReadUInt( 16 )
	
	Inventory[Name] = Amount

end)



That’s a simple way of doing it using the net library, then when you come to displaying it client side you would loop through it ( for k,v in pairs )

The key would be the item name and the value would be the amount of the time the player has.

Not exactly an inventory, but similar. :stuck_out_tongue:

Also how would the client be able see what he has in his inventory. Is there a function to get the server variables?

Why does the error message for every function say ‘AddItem Format’?

Probably just copy paste

Read the bottom of my post,

displaying it client side you would loop through it ( for k,v in pairs )

The key would be the item name and the value would be the amount of the time the player has.

For an example

Also yeah, i was kinda lazy with the asserting, my bad for CP’ing them.





Inventory = {
["Melons"] = 5;
["Apples"] = 4;
}

for k,v in pairs( Inventory ) do 
	print( k,v )
end

Output:
Melons 5
Apples 4