Jump to content

Transmitting and Requesting moddata problem


smajt

Recommended Posts

Hi, im trying to save bank account data as global moddata per player.
My problem is that when im withdrawing money every second time data is updated.

 

local function buttonClick(button, args)
    writeLog(ATM.GetLogName(), "debug0.0 - " .. ATM.Data.Account.money);
    num = tonumber(ui["amount"]:getValue())
    writeLog(ATM.GetLogName(), "debug0.1 - " .. num);
    if num then
        ATM.Sync.LoadRemote()
        writeLog(ATM.GetLogName(), "debug0.2 - " .. ATM.Data.Account.money);
        if args.action == "withdraw" and ATM.Data.Account.money >= num then
            ATM.Data.Account.money = ATM.Data.Account.money - num
            ModData.add(ATM.GetModDataStatsKey(), ATM.Data.Account);
            ATM.withdrawMoney(getPlayer(), num)
        --elseif args.action == "deposit" then
            -- logika depozytu
        end
    end
    updateBalance()
end

function ATM.withdrawMoney(playerObj, num)
    writeLog(ATM.GetLogName(), "debug1.0 - " .. ATM.Data.Account.money);
    while num > 0 do
        local max = 0
        local maxType = ""
        for itemType, data in pairs(ATM.Money.Values) do
            if num >= data.v then
                max = data.v
                maxType = itemType
            end
        end

        if max == 0 then break end

        playerObj:getInventory():AddItem(maxType)
        num = num - max
    end

    
    ATM.Sync.SaveRemote();
end

function ATM.Sync.SaveRemote()
    if not isClient() then
        return;
    end

    ModData.transmit(ATM.GetModDataStatsKey());
end

function ATM.Sync.LoadRemote()
    if not isClient() then
        return;
    end

    writeLog(ATM.GetLogName(), "load remote");
    ModData.request(ATM.GetModDataStatsKey());
end

local function OnClientInitGlobalModData()
    if not isClient() then
        return;
    end

    ATM.Sync.LoadRemote();
end

local function OnServerReceiveModData(key, modData)
    if isClient() or not String:StartsWith(key, ATM.Id) or not modData then
        return;
    end
    writeLog(ATM.GetLogName(), "debug3 - ".. modData.money);
    writeLog(ATM.GetLogName(), "saving mod data.");
    ModData.add(key, modData);
end

local function OnClientReceiveStatsModData(key, modData)
    if not isClient() or key ~= ATM.GetModDataStatsKey() then
        return;
    end

    if not modData then
        ATM.Data.Account = {money = 1000, transactions = {}}
        ModData.add(key, ATM.Data.Account);
        ATM.Sync.SaveRemote()
    else
        ATM.Data.Account = modData;
        ModData.add(key, ATM.Data.Account);
    end

    writeLog(ATM.GetLogName(), "saving player bank account");
end

Events.OnInitGlobalModData.Add(OnClientInitGlobalModData);
Events.OnReceiveGlobalModData.Add(OnServerReceiveModData);
Events.OnReceiveGlobalModData.Add(OnClientReceiveStatsModData);


here im pasting the logs of first two tries.

 

---- CLIENT LOGS ----

[25-01-24 21:25:56.787] debug0.0 - 400.
[25-01-24 21:25:56.788] debug0.1 - 100.
[25-01-24 21:25:56.788] load remote.
[25-01-24 21:25:56.788] debug0.2 - 400.
[25-01-24 21:25:56.788] debug1.0 - 300. <- this value should be in next debug0.0
[25-01-24 21:25:56.812] saving player bank account.
[25-01-24 21:26:17.379] debug0.0 - 400. <- should be 300
[25-01-24 21:26:17.380] debug0.1 - 100.
[25-01-24 21:26:17.380] load remote.
[25-01-24 21:26:17.380] debug0.2 - 400.
[25-01-24 21:26:17.380] debug1.0 - 300.
[25-01-24 21:26:17.396] saving player bank account.
---- SERVER LOGS ----

[25-01-24 21:25:56.797] debug3 - 300.
[25-01-24 21:25:56.797] saving mod data..
[25-01-24 21:26:17.388] debug3 - 300.
[25-01-24 21:26:17.388] saving mod data..



someone have an idea what is going wrong ? I dont know what could be a problem cause its only happening every second time i press the withdraw button.
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...