Whats wrong with this save script?

Hi, so I made a autosave script recently, I’m not the best at this because I mostly use save buttons for saving. This script won’t properly work, it does the prints and no warns but it still doesn’t load data. Did I miss something crucial? Thanks.

local DataStoreService = game:GetService("DataStoreService")
local CashStore = DataStoreService:GetDataStore("Cash")

game.Players.PlayerAdded:Connect(function(Player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player

    local Cash = Instance.new("IntValue")
    Cash.Name = "Cash"
    Cash.Value = 0
    Cash.Parent = leaderstats

    local data
    local Success, Error = pcall(function()
        data = CashStore:GetAsync(Player.UserId.."-cash")
    end)

    if Success then
        Cash.Value = data
        print("Data Found!")
    else
        warn(Error)
    end
end)
local queue = 0
game.Players.PlayerRemoving:Connect(function(Player)
    queue += 1
    local Sucess, Error = pcall(function()
        CashStore:SetAsync(Player.UserId.."-Cash", Player.leaderstats.Cash.Value)
    end)

    if Sucess then
        print("Data Saved!")
        queue -= 1
    else
        warn(Error)
    end
end)

game:BindToClose(function()
     repeat wait() until queue == 0
end)

You’re testing ingame right? PlayerRemoving doesn’t have time to execute in studio tests.

You have two separate keys.

1 Like

Omg I am so stupid, thanks alot. I was tearing myself apart on this.

1 Like

Nah this kinda thing happens all the time. Glad I could help spot it tho.

2 Likes