Sorry for late reply. DataSaving in my script actually means nothing. I don’t know why I added it there, but it’s only nil, and I never used it.
Thank you very much for answering! I had the doubt in my head, thank you!
I don’t understand this part well, why did you code: 'Coin_'
what is the reason for doing this?
There isn’t actually a reason why I did this. I don’t know why I do it, but I always do it. It just saves the player’s key like this: 'Coins_PlayerID'
When you save and load the player’s data, you have to make sure the key exists. So I also do that when loading it. I could just do DataStore:SetAsync(player.UserId, data)
but I like adding a Coins_
to the player’s key whenever I save and load the player’s data.
I haven’t tried this but I assume this won’t work if the player who is receiving the gift is playing the game at that moment right? Because you’re using SetAsync
and saving the current leaderstat Coins
value which would overwrite the gifted value. Also, any reason why you wouldn’t use UpdateAsync
? For example instead of this:
local success, err = pcall(function()
coins = CoinsDataStore:GetAsync('Coins_'..Id)
end)
if success then
local success2, err2 = pcall(function()
CoinsDataStore:SetAsync('Coins_'..Id, coins + amount)
end)
end
You could have this
local success, err = pcall(function()
coins = CoinsDataStore:UpdateAsync('Coins_'..Id, function(oldValue)
return oldValue + amount
end)
end)