• Syncing metatables across client and server...
    4 replies, posted
so i would expect metatables to be updated one one end also updated on the other... nope. if i set a player's squad (networked string) on server, client still says it's nil. do i have to use networking / usermessages or am i missing something? there arent really any good metatable tutorails
Uhhh, I think you're misusing the word 'metatable'. Are you saying you're doing something like ply.Squad = "Cops" on the server but it's not setting it on the client as well? If so then that is the intended result. To network a variable between server and client use either [url=http://wiki.garrysmod.com/page/Networking_Entities]Datatable Vars[/url] or [url=http://wiki.garrysmod.com/page/Special:Search?search=SetNW*&fulltext=Search]NWVars[/url].
[QUOTE=mcd1992;47640005]Uhhh, I think you're misusing the word 'metatable'. Are you saying you're doing something like ply.Squad = "Cops" on the server but it's not setting it on the client as well? If so then that is the intended result. To network a variable between server and client use either [url=http://wiki.garrysmod.com/page/Networking_Entities]Datatable Vars[/url] or [url=http://wiki.garrysmod.com/page/Special:Search?search=SetNW*&fulltext=Search]NWVars[/url].[/QUOTE] No, this is what I'm doing: 1. include and AddCSLuaFile sh_meta.lua sh_meta.lua: [lua]local plyMeta = FindMetaTable("Player"); function plyMeta:SetClass(class) self:SetNetworkedInt("Class", class); end function plyMeta:Class() return self:GetNetworkedInt("Class"); end function plyMeta:SetSquad(squad) self:SetNetworkedString("Squad", squad); end function plyMeta:Squad() return self:GetNetworkedString("Squad"); end[/lua] Setting and reading squads and classes works 100% on the server, but on the client, calling LocalPlayer:Squad() results in a nil value.
Hrm, I think your code should work but the SetNetworkedString function is depricated now so I'd just get rid of it. Try using [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/SetNWString"]Entity:SetNWString[/URL] instead? [editline]1st May 2015[/editline] Also make sure you're not calling SetSquad on the client as this will cause the client to get desynced from the server. Unless you're certain that they are the same value.
[QUOTE=mcd1992;47640178]Hrm, I think your code should work but the SetNetworkedString function is depricated now so I'd just get rid of it. Try using [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/Entity/SetNWString"]Entity:SetNWString[/URL] instead? [editline]1st May 2015[/editline] Also make sure you're not calling SetSquad on the client as this will cause the client to get desynced from the server. Unless you're certain that they are the same value.[/QUOTE] entity? as in i need to create an entity for this or as in ply:SetNWString? do i have to abandon metatables? EDIT: SetNWString works perfectly!
Sorry, you need to Log In to post a reply to this thread.