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

Saving leaderboard isn't working properly?

Asked by 4 years ago

It is printing "Success!" and "Data Loaded" but it isn't actually loading the data.

Code:

local serverStorage = game:GetService("ServerStorage")
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerSave")

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

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

    local steps = Instance.new("NumberValue")
    steps.Name = "Steps"
    steps.Parent = f

    local rebirths = Instance.new("IntValue")
    rebirths.Name = "Rebirths"
    rebirths.Parent = f

    local races = Instance.new("IntValue")
    races.Name = "Races"
    races.Parent = f

    local dataFolder = Instance.new("Folder")
    dataFolder.Name = plr.Name
    dataFolder.Parent = serverStorage.RemoteData

    local stepsData, rebirthsData, racesData

    local success, errormsg = pcall(function()
        stepsData = datastore:GetAsync("Steps-"..plr.UserId)
        rebirthsData = datastore:GetAsync("Rebirths-"..plr.UserId)
        racesData = datastore:GetAsync("Races-"..plr.UserId)
    end)

    if success then
        if stepsData then
            steps.Value = stepsData
            rebirths.Value = rebirthsData
            races.Value = racesData
            print("Data Loaded")
        end
    else
        warn(errormsg)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local success, errormsg = pcall(function()
        datastore:SetAsync("Steps-"..plr.UserId, plr.leaderstats.Steps.Value)
        datastore:SetAsync("Rebirths-"..plr.UserId, plr.leaderstats.Rebirths.Value)
        datastore:SetAsync("Races-"..plr.UserId, plr.leaderstats.Races.Value)
    end)

    if success then
        print("Success!")
    else
        warn(errormsg)
    end
end)
0
There are no errors in the output either. Scryptol 2 — 4y
1
No no no no no no no no no no no no save tables not 3 times use GetAsync, make a table and save all the values in it, it is much more efficient. I reccomend you going to official devforum, there is a tutorial for data stores. imKirda 4491 — 4y
0
Okay, thanks Scryptol 2 — 4y

Answer this question