Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Existing players cant have new money accounts #1110

Open
avilchiis opened this issue May 25, 2024 · 3 comments
Open

[BUG] Existing players cant have new money accounts #1110

avilchiis opened this issue May 25, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@avilchiis
Copy link

Summary

If you register a new money account, all existing players won't have it only new characters

Reproduction

1 - Register a new money account in qb-core/config.lua > QBConfig.Money.MoneyTypes and QBConfig.Money.DontAllowMinus, name it however you want, in my case I used "cosmo".

2 - Add the following command to any server.lua (ofc make sure to load QBCore object first)

RegisterCommand("test", function(source,args)
    local Player = QBCore.Functions.GetPlayer(source)
    local account = args[1]
    print("account: "..account, "amount: ", Player.PlayerData.money[account])
end)

3 - Use the command /test [account name] and it should print your current balance, it prints 0 if the account exists and it'll print nil if the account doesn't exist in the player data.

4 - Logout and create a new character

5 - Use the command with the new character, cosmo will be printed with a 0 instead of nil.

6 - Verify the players table and the old character doesn't have the new accounts, the new one does have it.

image

Expected behavior

Print 0 because money account exists but balance is 0, this is the behavior in previous versions of qb-core, I'm currently running one from April which works ok, all new accounts are added to existing and new players but the most recent version of qb-core is not adding them.

Actual behavior

It prints nil meaning the account doesn't exist and is not adding the account to the player money table in database

Additional context

No clue how to solve it tbh, I think is has something to do with the applyDefaults function

Last Updated

today

Custom Resources

None

Resource Rename

qb-core

@avilchiis avilchiis added the bug Something isn't working label May 25, 2024
@avilchiis
Copy link
Author

avilchiis commented May 25, 2024

I found a solution for this, needs to modify the applyDefaults function in qb-core/server/player.lua line 80 with this:

local function applyDefaults(playerData, defaults)
    for key, value in pairs(defaults) do
        if type(value) == 'function' then
            if key == "money" then
                local moneyTypes = value()
                if not playerData[key] then
                    playerData[key] = moneyTypes
                else
                    for account, balance in pairs(moneyTypes) do
                        playerData[key][account] = playerData[key][account] or balance
                    end
                end
            else
                playerData[key] = playerData[key] or value()
            end
        elseif type(value) == 'table' then
            playerData[key] = playerData[key] or {}
            applyDefaults(playerData[key], value)
        else
            playerData[key] = playerData[key] or value
        end
    end
end

Don't kill me I don't know how to do a pull request 😆, I test this with an existing character and with a new one, works fine with both, 0 errors found 👀

@avilchiis
Copy link
Author

Can you test if the following code works for your test cases?

local function applyDefaults(playerData, defaults)
    for key, value in pairs(defaults) do
        playerData[key] = type(value) == 'function' and
            (key == "money" and playerData[key] or value()) or
            type(value) == 'table' and applyDefaults(playerData[key] or {}, value) or
            playerData[key] or value
    end
    return playerData
end

No it doesn't work and for some reason your code duplicates the character in database but with a different citizenid

Copy link

github-actions bot commented Sep 9, 2024

This issue has had 60 days of inactivity & will close within 7 days

@github-actions github-actions bot added Stale and removed Stale labels Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant