Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my IntValue resetting when the game is exited?

Asked by 2 years ago

My game has an IntValue that is meant to be used for save and load, but instead it resets to the saved value on game exit: Example: I use DataStore Editor to set my value to 50. Then I play the game and set my value to 55 (with explorer not DataStore Editor). Then I leave and test again. My IntValue is back at 50. Why is this happening? Code:

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DAT")

game.Players.PlayerAdded:Connect(function(player)
    local coins = Instance.new("IntValue")
    coins.Name = "coins"
    coins.Parent = player

    local playerUserId = "Player_"..player.UserId
    print(playerUserId)
    local data1
    local success, err = pcall(function()
        data1 = dataStore:GetAsync(playerUserId)
    end)
    print(data1)
    player.coins.Value = data1

    if success then
        print("Load success!")
        game.StarterGui.ScreenGui.TextButton.Text = player.coins.Value
    else
        warn(err)
    end
    print(player.coins.Value)
end)

game.Players.PlayerRemoving:Connect(function(player)
    print(player.coins.Value)
    local success, err = pcall(function()
        local playerUserId = "Player_"..player.UserId
        print(playerUserId)
        dataStore:SetAsync(playerUserId, player.coins.Value)
    end)

    if success then
        print("Save success!")
    else
        warn(err)
    end
end)

game:BindToClose(function()
    wait(5)
end)

Answer this question