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

Data As Table Not Saving. How To Fix?

Asked by 3 years ago

This is my first time working with saving a table for multiple data types. The problem is the data doesn't save. I tried to fix this by using bind to close, but it still doesn't work.

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local PlayerDataStore = DataStoreService:GetDataStore("PlayerData0.0.1")

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

    local Currency = Instance.new("NumberValue")
    Currency.Name = "Coins"
    Currency.Parent = PlayerInfo

    local Wins = Instance.new("NumberValue")
    Wins.Name = "Wins"
    Wins.Parent = PlayerInfo

    local Kills = Instance.new("NumberValue")
    Kills.Name = "Kills"
    Kills.Parent = PlayerInfo

    local SavedData = nil
    pcall(function()
        SavedData = PlayerDataStore:GetAsync(Player.UserId)
        print("a")
    end)

    if SavedData ~= nil then
        Currency.Value = SavedData["Currency"]
        Wins.Value = SavedData["Wins"]
        Kills.Value = SavedData["Kills"]
        print("b")
    else
        Currency.Value = 100
        Wins.Value = 0
        Kills.Value = 0
        print("c")
    end
end)

Players.PlayerRemoving:Connect(function(Player)
    local NewData = {}
    for _, Data in pairs(Player.Info:GetChildren()) do
        NewData[Data.Name] = Data.Value
    end

    PlayerDataStore:SetAsync(Player.UserId, NewData)
    print("d")
end)

game:BindToClose(function()
    for _, Player in pairs(Players:GetPlayers()) do
        if Player then
            Player:Kick("This server is shutting down.")
        end
    end
    wait(1)
    print("e")
end)

1 answer

Log in to vote
0
Answered by 3 years ago

I think in your line 8, you should name it from "info" to "leaderstats" I do think that will solve your problem, if you're not seeing the saved data.

Besides, I don't see anything wrong with the Datastore script.

Ad

Answer this question