#Two Datastore scripts, only one works.

2 messages · Page 1 of 1 (latest)

vernal lava
#

So I have 2 datastore scripts, one for leaderstats, and one for datastores I don't want to be shown in leaderstats. Only the leaderstats one works. Here are the scripts:
(Check comments)

#
  1. Leaderstats script:
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("DataStoreLeaderstats1")

local data = {
    ["Jumps"] = 0,
    ["Rebirths"] = 0
}

Players.PlayerAdded:Connect(function(Player)
    local DataFromStore = nil

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player

    for name, value in pairs(data) do
        local new = Instance.new("NumberValue")
        new.Name = name
        new.Value = value
        new.Parent = leaderstats
    end

    local s, e = pcall(function()
        DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
    end)

    if s then
        print("Getting Data For: " .. Player.Name)
        if DataFromStore then
            for name, value in pairs(DataFromStore) do
                Player.leaderstats[name].Value = value
            end
            print("Data Loaded For: " .. Player.Name)
        else
            print("Created New Data For: " .. Player.Name)
        end
    else 
        warn("Error Getting Data For: " .. Player.Name)
    end
end)

Players.PlayerRemoving:Connect(function(Player)
    local DataForStore = {}

    for name, value in pairs(Player.leaderstats:GetChildren()) do
        DataForStore[value.Name] = value.Value
    end

    local success = pcall(TestDataStore.SetAsync, TestDataStore, "uid-" .. Player.UserId, DataForStore)

    if success then
        print("Successfully Saved Data For: " .. Player.Name)
    end
end)