Glua/lua, sharing data between client and server

722 Views Asked by At

I completely understand if this post is a waste of time, as glua(Garrysmod the game) is a niche area, and there isn't too many resources out there. I recently moved back to Lua a few days ago, after first attempting in 2013 when I was young and didn't know much.

Now nearly ten years latter, I am back to give it a second go and this time plan on succeeding. I'm new to Lua but not programming in general, I also haven't touched a scripting language in a while.

I'm struggling to figure out an issue I am having with server/client player entity, and quite frankly i don't understand what is happing, I'm modifying a codebase, and am I trying to add a new variable to the player.

The SERVER code

function LDRP.InitializeSelf(ply,cmd,args)
    if ply.Inited then return end
    ply:LiquidChat("GAME", Color(0,200,200), "Your client has been fully established.")
    ply.Inited = true
    ply.Growing = {}
    
    local InvWeight = 0 
    -- Inventory:
    if ply:LiquidFile("Inventory") then
        local Tbl = von.deserialize(ply:LiquidFile("Inventory",true))
        ply.Inventory = Tbl
        for k,v in pairs(Tbl) do -- Remove unexisting items and recalculate weight
            if !Check[k] then ply.Inventory[k] = nil continue end
            
            if v >= 1 then
                InvWeight = InvWeight+(Check[k].weight*v)
            end
        end
    else
        ply.Inventory = {}
    end
    
    timer.Simple(3,function()
        if !ply:IsValid() then return end
        for k,v in pairs(ply.Inventory) do
            ply:SendItem(k,v)
        end
        timer.Simple(1,function()
            if !ply:IsValid() then return end
            for k,v in pairs(ply.Character.Bank) do
                ply:SendBItem(k,v)
            end
        end)
    end)

    -- Skills:
    if ply:LiquidFile("Character") then
        ply.Character = von.deserialize(ply:LiquidFile("Character",true))
        ply.Character.InvWeight["cur"] = InvWeight
    else
        ply.Character = {}
        ply.Character.Skills = {}
        ply.Character.InvWeight = {["cur"] = 0,["allowed"] = 10}
    end

    if !ply.Character.BankWeight then
        ply.Character.Bank = {["curcash"] = 0}
        ply.Character.BankWeight = {["cur"] = 0,["allowed"] = 30}
    ply.Character.InterestRate = {["cur"] = 0}
    end
    
    timer.Simple(3,function()
        if !ply:IsValid() then return end
        umsg.Start("SendWeight",ply)
            umsg.Float(ply.Character.InvWeight.allowed)
        umsg.End()
        umsg.Start("SendBWeight",ply)
            umsg.Float(ply.Character.BankWeight.allowed)
        umsg.End()
    end)
    timer.Simple(2,function()
        if !ply:IsValid() then return end
        if !file.Exists("liquiddrp/didtutorial_" .. ply:UniqueID() .. ".txt", "DATA") then
            ply:DoTutorial("start")
            
            file.Write("liquiddrp/didtutorial_" .. ply:UniqueID() .. ".txt","1")
        end
    end)
    
    for k,v in pairs(LDRP_SH.AllSkills) do
        if !ply.Character.Skills[k] then
            ply.Character.Skills[k] = {}
            ply.Character.Skills[k].lvl = 1
            ply.Character.Skills[k].exp = 0
        end
    end
    
    timer.Simple(.7,function()
        if !ply:IsValid() then return end
        for k,v in pairs(ply.Character.Skills) do
            umsg.Start("SendSkill",ply)
                umsg.String(k)
                umsg.Float(v.exp)
                umsg.Float(v.lvl)
            umsg.End()
        end
    end)
    ply:SavePlayer()
end
concommand.Add("_initme",LDRP.InitializeSelf)

The CLIENT init code

local LDRP = {}

function LDRP.InitClient()

    timer.Simple(1, function() 
        
        LocalPlayer().Inventory = {}

        LocalPlayer().NewItemsLog = {}

        LocalPlayer().Skills = {}

        LocalPlayer().Bank = {}

        LocalPlayer().InterestRate = {}

        print("Client has initialized")

        RunConsoleCommand("_initme")

    end)
end

hook.Add("InitPostEntity", "LoadCharacter", LDRP.InitClient)

The issue I am having is I can't access the LocalPlayer().InterestRate table on the client, though the others like LocalPlayer().Bank work fine and can be accessed, but they are part of the original code base which I am modifying.

I have been banging my head against the wall, searching through the code to figure out why the original values work, and can be accessed on the LocalPlayer, but the single one I added can't. It's being saved, and loaded on the server, just not on the client.

I'm sure the client init function, is not the only one that set's the LocalPlayer() variables, because I can't see how the true variables get assigned when I am setting them to an empty table. Another thing, on the server it's setting them like ply.Character.Bank = {}, so how are they being set to LocalPlayer().Bank and completely skipping the Character table.

I may be being dumb and blind, and completely missing the issue, but I honestly have spent a long time, searching through every file finding related lines, but it's only these I can see that are declaring and assigning the values.

Here is the GitHub repo if that helps.

https://github.com/carlbeattie2000/rebellionrp

I appreciate anyone who gives advice/guidance.

Thank you

0

There are 0 best solutions below