Datastore saves the player cash but when I change the Value to 0 and rejoin the leaderstats still shows 1000

  1. What do you want to achieve? Keep it simple and clear!

I am trying to make like a currency system and the new player will be given 1000 and a datastore that saves all the remaining cash if they bought something from the store.

  1. What is the issue? Include screenshots / videos if possible!

the datastore does save but when I rejoin I still got 1000 even though I have already set the Value to 0 in the script.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried researching it on google and developer forum but I could not find a way through to fix this…

local VND = "VND"
local DataStore = game:GetService("DataStoreService"):GetDataStore("VND")
game.Players.PlayerAdded:Connect(function(player)

    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = player

    local currency = Instance.new("IntValue")
    currency.Name = VND
    currency.Parent = folder
    currency.Value = 0

    local ID = VND..("-")..player.UserId
    print(ID)
    local data = DataStore:GetAsync(ID)
    
    if data then
        currency.Value = data
        print("data loaded")
    else
        currency.Value = 0
    end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, err = pcall (function()
        local ID = VND..("-")..player.UserId
        DataStore:SetAsync(ID, player.leaderstats.VND.value)
    end)
    if not success then 
        warn('could not save data')
    end
end)

I really hope someone could help me out
Thanks!

Test it in game, this script should work there. This is because studio doesnt save unless you use BindToClose() (assuming you are even using studio as I see no problem with the script).

I have tried it in game the datastore saves but when I go to studio and change the value to 0 and publish the game I rejoin and still got 1000 cash :confused:

Make sure you change the points from the server and not the client

1 Like

you have to go and change the player cash in the server or it wont be saved if changed through client

:OOOO Omg it works!! Tysm!! :smiley: