This datastore script shows no errors, but doesn't work. Can someone fix it?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SaveStats")

local function Load(plr)
    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"
    stats.Parent = plr

    local Money = Instance.new("IntValue")
    Money.Name = "Money"
    Money.Parent = stats

    local Level = Instance.new("IntValue")
    Level.Name = "Level"
    Level.Parent = stats

    local key = "plr-"..plr.UserId
    local saved

    local success, err = pcall(function()
        saved = DataStore:GetAsync(key)
    end)

    if not success then
        warn("Failed to read data"..tostring(err))
        return
    end

    if saved then
        Money.Value = saved.Coins
        Level.Value = saved.Bux
    else
        local statsToBeOverwritten = {
            ["Money"] = Money.Value;
            ["Level"] = Level.Value
        }

        local succes, er = pcall(function()
            DataStore:SetAsync(key, statsToBeOverwritten)
        end)

        if not succes then
            warn("Failed to overwrite data"..tostring(er))
            return
        end
    end
end

local function Save(plr)
    local key = "plr-"..plr.UserId

    local statsToBeOverwritten = {
        ["Money"] = plr.leaderstats.Money.Value;
        ["Level"] = plr.leaderstats.Level.Value
    }

    local success, err = pcall(function()
        DataStore:SetAsync(key, statsToBeOverwritten)
    end)

    if not success then
        warn("Failed to overwrite data"..tostring(err))
        return
    end
end

Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)

2 comments

0

One lines 54 and 55 you are saving at the indices of Money and Level. When you load the data on lines 31 and 32 you are loading from the indices of Coins and Bux, which are nil.

0

This is not constructive as you're only requesting a fix. You also just effortlessly copied this script from zblox164 https://scriptinghelpers.org/questions/70452/i-have-tried-lots-of-different-saving-scripts-and-none-of-them-work-can-someone-fix-this-one