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

My DataStore script will not save [?]

Asked by 3 years ago
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("TheData")
local warns = game.StarterGui.Warnings
local warnings = warns.Frame.WarnText



game.Players.PlayerAdded:Connect(function(player)


    local stats = Instance.new("Folder",player)
    stats.Name = "leaderstats"

    local coins = Instance.new("IntValue",stats)
    coins.Name = "Coins"
    coins.Value = 100

    local levels = Instance.new("IntValue", stats)
    levels.Name = "Levels"
    levels.Value = 1
    local exp = Instance.new("IntValue", stats)
    exp.Name = "Exp"
    exp.Value = 0
    local mycafeval = Instance.new("NumberValue", stats)
    mycafeval.Name = "OwnsCafe"
    mycafeval.Value = 0


    local data
    local success, err = pcall(function()
        data = dataStore:GetAsync(player.UserId)
    end)

    if success then 
        coins.Value = data[1]
        levels.Value = data [2]
        exp.Value = data [3]

    else
        print("Player has NO data!")
        warnings.Text = "No data to be saved!"

    end

end)
local function saveData(player)
    local tableForSaving = {
        player.leaderstats.coins.Value,
        player.leaderstats.levels.Value,
        player.leaderstats.exp.Value
    }

    local success, err = pcall(function()
        dataStore:SetAsync(player.UserId, tableForSaving)

    end)

    if success then
        print("Data has been saved")
        if success then
            print("Data has been saved")
            warnings.Text = "Data has been saved!"

        else
            print("Failed To Save Data")
            warnings.Text = "Data has not been saved!"
            warn(err)
        end
    end

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
    local success, err  = pcall(function()
        saveData(player) -- Save the data
        end)

        if success then
        print("Data has been saved")
        warnings.Text = "Data has been saved!"
        else
        print("Data has not been saved!")
        warnings.Text = "Data has not been saved!"

    end
end)

game:BindToClose(function() -- When the server shuts down
    for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
        local success, err  = pcall(function()
            saveData(player) -- Save the data
        end)

        if success then
            print("Data has been saved")
            warnings.Text = "Data has been saved!"
        else
            print("Data has not been saved!")
            warnings.Text = "Data has not been saved!"
        end
    end
end)

This is my DataSave script but it will not save

0
Please provide more context. deeskaalstickman649 475 — 3y
0
My script won't work and I don't know why what other context do you need DiOrOmG2 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Idk sure but you should try to separe the data save from leaderstats. It should work then.

Ad

Answer this question