Question about sending tables between server and client.
1 replies, posted
First off, here is my code;
[B]Server:[/B]
[CODE]
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
[/CODE]
[B]Shared:[/B]
[CODE]
local plyMeta = FindMetaTable( "Player" )
function plyMeta:getParty()
local plyID = self:AccountID()
if parties[plyID] then
return "true"
end
end
[/CODE]
[B]Client:[/B]
[CODE]Simply using ply:GetParty().name[/CODE]
[B]It throws this error:[/B]
[CODE]
[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
[/CODE]
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:
[CODE]
function plyMeta:getPartyName()
return parties[self:AccountID()].name
end
[/CODE]
But I realize it only allows the server instance of the shared file to access it, not the client instance.
Sorry, you need to Log In to post a reply to this thread.