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

Anyone Got Any Idea How I Can Get My SaveData Script To Work With The Leaderboard Script?

Asked by
Fxicity 10
3 years ago

Im not sure what values to change to what.

heres the LeaderboardHandler

local ds = game:GetService("DataStoreService")

local PointsODS = ds:GetOrderedDataStore("leaderstats")

local timeUntilReset = 10

while wait(1) do

    timeUntilReset = timeUntilReset - 1

    script.Parent.Parent.Additional.ResetTimeT.Text = "Resetting In " .. timeUntilReset .. " Seconds..."

    if timeUntilReset == 0 then

        timeUntilReset = 10

        for i, plr in pairs(game.Players:GetPlayers()) do

                PointsODS:SetAsync(plr.UserId, plr.leaderstats.Points.Value)

            end

        for i, leaderboardRank in pairs(script.Parent==:GetChildren()) do

            if leaderboardRank.ClassName == "Frame" then
                leaderboardRank:Destroy()

            end

        end

        local success, errorMsg = pcall(function()

            local data = PointsODS:GetStoredAsync(false, 50)
            local PointsPage = data:GetCurrentPage()

            for rankInLB, dataStored in ipairs(PointsPage) do

                local Name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.savevalue1))
                local Points = dataStored.value

                local template = script.Template:Clone()

                template.name = Name .. "Leaderboard"

                template.PlrName.Text = Name

                template.Rank.Text = "#" .. rankInLB

                template.Points.Text = Points

                template.Parent = script.Parent

            end

        end)

    end

endlocal ds = game:GetService("DataStoreService")

local PointsODS = ds:GetOrderedDataStore("leaderstats")

local timeUntilReset = 10

while wait(1) do

    timeUntilReset = timeUntilReset - 1

    script.Parent.Parent.Additional.ResetTimeT.Text = "Resetting In " .. timeUntilReset .. " Seconds..."

    if timeUntilReset == 0 then

        timeUntilReset = 10

        for i, plr in pairs(game.Players:GetPlayers()) do

                PointsODS:SetAsync(plr.UserId, plr.leaderstats.Points.Value)

            end

        for i, leaderboardRank in pairs(script.Parent==:GetChildren()) do

            if leaderboardRank.ClassName == "Frame" then
                leaderboardRank:Destroy()

            end

        end

        local success, errorMsg = pcall(function()

            local data = PointsODS:GetStoredAsync(false, 50)
            local PointsPage = data:GetCurrentPage()

            for rankInLB, dataStored in ipairs(PointsPage) do

                local Name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.savevalue1))
                local Points = dataStored.value

                local template = script.Template:Clone()

                template.name = Name .. "Leaderboard"

                template.PlrName.Text = Name

                template.Rank.Text = "#" .. rankInLB

                template.Points.Text = Points

                template.Parent = script.Parent

            end

        end)

    end

end

heres the savedata

local DS = game:GetService("DataStoreService"):GetDataStore("SaveMyData")
game.Players.PlayerAdded:Connect(function(player)
    wait()
    local playerkey = "id_"..player.userId
    local savevalue = player.leaderstats.Points
    local savevalue2 = player.leaderstats.Prestige

    local GetSaved = DS:GetAsync(playerkey)
    if GetSaved then
        savevalue.Value = GetSaved[1]
        savevalue2.Value = GetSaved[2]
    else
        local NumbersForSaving = {savevalue.Value, savevalue2.Value}
        DS:GetAsync(playerkey, NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    DS:SetAsync("id_"..player.userId, {player.leaderstats.Points.Value, player.leaderstats.Prestige.Value})
end)

heres the leaderstats

game.Players.PlayerAdded:Connect(function(player)
     local leaderstats = Instance.new("Model")
     leaderstats.Name = "leaderstats"
     leaderstats.Parent = player

local money = Instance.new("IntValue") 
     money.Name = "Points" 
     money.Value = 0 
     money.Parent = leaderstats 
local money = Instance.new("IntValue")
     money.Name = ("Prestige")
     money.Value = 0 
     money.Parent = leaderstats  
local money = Instance.new("BoolValue") 
     money.Name = "DoublePass" 
     money.Value = false
     money.Parent = player
end)
0
Please respond as this will be an update to my game Fxicity 10 — 3y
0
is this your code User#30567 0 — 3y
0
yes Fxicity 10 — 3y
0
i got the leaderboard from a tut because i had no clue how to make one Fxicity 10 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

This is quite simple, and honestly isn't all that hard!

First you're going to want to run a player added event obviously, and you're gonna wanna wrap it in a pcall for extra errors that get caught or incase roblox data centers go down you don't want data being lost, you just want it being caught. You're going to want to put your data stores at the top.

local Ds = game:GetService("DataStoreService") -- Variable name can be changed but the argument cannot.
local DataSave = Ds:GetDataStore("NameOfDataStore") -- Can be anything.

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder") -- Creates a folder for it to be stored.
    leaderstats.Parent = plr -- Parents it to the player
    leaderstats.Name = "leaderstats" -- Sets the name of the leaderstats

    local Currency = Instance.new("IntValue") -- Change to any value you'd like.
    Currency.Parent = leaderstats
    Currency.Name = "Money" -- Put currency name here
    Currency.Value = 0 -- Change to any value.
end)

After you do this you will do the following:

local Ds = game:GetService("DataStoreService") -- Variable name can be changed but the argument cannot.
local DataSave = Ds:GetDataStore("NameOfDataStore") -- Can be anything.

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder") -- Creates a folder for it to be stored.
    leaderstats.Parent = plr -- Parents it to the player
    leaderstats.Name = "leaderstats" -- Sets the name of the leaderstats

    local Currency = Instance.new("IntValue") -- Change to any value you'd like.
    Currency.Parent = leaderstats
    Currency.Name = "Money" -- Put currency name here
    Currency.Value = 0 -- Change to any value.

    local Save = "Money_"..plr.UserId -- Your data store needs a key to be identified with, so you're going to have to use a key in this case I use "Money_"..plr.UserId, you can use "Coins-" or "Coins." etc whatever the name of your currency is, is what you use. We call this later.
    local data -- This can be called later aka below.
    local succsess, err = pcall(function() -- This will catch errors, and also when data centers go down they will keep all data to itself..
        data = MyData:GetAsync(Save) -- Get the async of the data, and set it to the player id and the currency value.
    end)

    if succsess then -- Checking if it's a success then we will do stuff.
        Currency.Value = data -- If it's a success we're setting the currency value to the data because that's what we're saving.
        print("Got Data") -- Prints so we know.
    else -- Simple else statement, connects 2 things.
        print(err) -- If there is an error, it will print it for you.
    end

    while true do -- Never ending loop (this whole part is not needed)
        Currency.Value = Currency.Value + 1 -- Sets the value to + 1 every second.
        wait(1) -- Waits 1 so the game does not bug out.
    end
end)

Now onto the last step in this process.

game.Players.PlayerRemoving:Connect(function(plr) -- Player is removed, it will save all the data.
    local Save = "Money_"..plr.UserId -- That key I talked about earlier? Yeah, we're picking it up again to save here!
    local success, errorMsg = pcall(function() -- Another pcall that catches errors, and keep's data.
        MyData:SetAsync(Save, plr.leaderstats.Money.Value) -- We're going to SAVE the Async now, and we're saving the Players ID and also the money value because that's what is being added, you can save multiple by adding a , and doing *plr.leaderstats.Coins.Value* or *plr.leaderstats.Rebirths.Value* this will save all of it.
    end)

    if success then -- If it's a success then it will print the thing below.
        print("Saved Amazingly!") -- Prints this if it's a success.
    else -- Another else statement, this can be used in an elseif and say elseif err then print("Error") but we do it like this so we get a clear function of what the error is!
        print(errorMsg) -- Else it's going to print the error message that happened.
    end
end)

For the leaderboard, you wanna connect it to your value that your leaderstat has and then you can display the amount they have on a text label, everything seems fine. I wrote the data store above for you just incase!

Ad

Answer this question