It seems like datastorePlayerdata:GetAsync is not correctly returning the default values. Perhaps there is bad player data present in the store without a wallet field left over from your previous testing? It would be helpful to see the GetAsync code as well.
If the player joins the first time, they have no saved data. It seems you’re setting the default values anyways? So there is not point in doing :GetAsync(player.UserId, defaultPlayerdata, {"bank", "wallet"}).
Just do :GetAsync(player.UserId) and check if the data exists before setting it:
local playerData = datastorePlayerdata:GetAsync(player.UserId)
if playerData then
--code here
end
I would also wrap the :GetAsync() in a pcall incase it errors
Also when you save, make sure the indexes in the newPlayerdata are the exact same as the ones where you load data. So I would change newBank to bank and newWallet to wallet.
Unless it hasn’t been updated in a while, from the normal API page the GetAsync function only accepts a key parameter, the rest is just wasted parameters. But correct me if I’m wrong…
Ah, right. In my reply above I assumed that he was using a data store wrapper module with default values. That is why I asked for the source. Looking closer it seems like a normal datastore, in which case I would agree with you: it only takes the key as a parameter.
Two of the variables you pass are wasted. “defaultPlayerdata” and {“bank”, “wallet”}.
If you remove those, it probably won’t immediately fix your problem but putting those variables there anyways is useless.
What is usually done with GetAsync() is to get the data with a singular key, that being the player’s user id. From there they check if the data that is related to that key, the user id, exists. If it doesn’t exist, it creates a new data folder with default values for the player.
This is what your code is missing. Your code checks if the data exists, but if the data doesn’t exist (like a new player joining your game for the first time) then it doesn’t do anything.
else
bankValue.Value = 4000
walletValue.Value = 0
end
These lines are reached when “playerData == nil”, which occurs when there is no data for a given player (means that the player is joining for the first time). These values are never used before the first time you save data, since you save data with the line
So this means that the first time you save data, you’re saving the “defaultPlayerdata” of
{
bank = 4000,
wallet = 0,
}
as the data for your user. Then, each new time you’re getting the data for the same user, the data for that user has bank=4000, wallet=0, because this is what you saved. So this means that the lines
aren’t doing anything, since playerData.bank=4000 and playerData.wallet=0, but defaultPlayerdata.bank was already 4000 and defaultPlayerdata.wallet was already 0. Basically, you’ve never changing the values of “defaultPlayerdata”.