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

Can I have help with my leaderboard stats please?

Asked by 1 year ago
Edited 1 year ago

Hey, I am creating an obby but I have this problem: I want the stage leaderboard and the minute leaderboard In the top right of the screen. The problem is, when I put the minutes leaderboard in Serverscript service, I only have the stage leaderboard who is showing but not the minutes leaderboard and the leaderboard dont update, there a video the to understand my problem: https://streamable.com/jux3pk and theres the two scripts: -the stage leaderboard:

local checkpoints = workspace:WaitForChild("Checkpoints droite")
local remoteEvent = game.ReplicatedStorage:WaitForChild("ResetStatEvent") 

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

    local stage = Instance.new("IntValue")
    stage.Name = "Stage"
    stage.Value = 0
    stage.Parent = leaderstats

    player.CharacterAdded:Connect(function(char)
        local hum = char:WaitForChild("Humanoid")
        wait()
        char:MoveTo(checkpoints[stage.Value].Position)

        hum.Touched:Connect(function(hit)
            if hit.Parent == checkpoints then
                if tonumber(hit.Name) == stage.Value + 1 then
                    stage.Value = stage.Value + 1
                end
            end
        end)
    end)
end)

remoteEvent.OnServerEvent:Connect(function(plr)
    if plr and plr.Character and plr.Character:FindFirstChild("Humanoid") then
        local humanoid = plr.Character.Humanoid
        plr.leaderstats.Stage.Value = 0
        humanoid.Health = 0
    end
end)

and the minutes leaderboard:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")

game.Players.PlayerAdded:Connect(function(Player)
    local Leaderstats = Instance.new("Folder")
    Leaderstats.Name = "leaderstats"
    Leaderstats.Parent = Player
    local Minutes = Instance.new("IntValue")
    Minutes.Name = "Minutes"
    Minutes.Value = 0
    Minutes.Parent = Leaderstats

    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        Minutes.Value = Data
    end

    coroutine.resume(coroutine.create(function()
        while true do
            wait(60)
            Minutes.Value = Minutes.Value + 1
        end
    end))
end)

game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.leaderstats.Minutes.Value)
end)

Thanks in advance!

2 answers

Log in to vote
1
Answered by
9mze 193
1 year ago
Edited 1 year ago
local DataStoreService = game:GetService("DataStoreService")
local MarketplaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local checkpoints = workspace:WaitForChild("Checkpoints droite")
local minutesDataStore = DataStoreService:GetDataStore("TimeStats")
local remoteEvent = ReplicatedStorage:WaitForChild("ResetStatEvent")
local stageDataStore = DataStoreService:GetDataStore("stageStore")

local function loop(callback, attempts, waitDuration, ...)
    local _, success, result
    for i = 1, attempts or 1 do
        _, success, result = pcall(callback)
        if success then
            break
        end
        if waitDuration then
            wait(waitDuration)
        end
    end
    return success, result
end
local function loaded(plr, instance, success, result)
    if success then
        instance.Value = result
        print(plr.Name .. "'s data was successfully retrieved.")
    else
        warn(plr.Name .. "'s data had an issue loading.")
        warn(result)
    end
end
local function saved(plr, success, result)
    if success then
        print(plr.Name .. "'s data was successfully saved.")
    else
        warn(tostring(plr.Name) .. "'s data had an issue saving.")
        warn(result)
    end
end

Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local stage = Instance.new("IntValue", leaderstats)
    stage.Name = "Stage"
    stage.Value = 0

    local minutes = Instance.new("IntValue", leaderstats)
    minutes.Name = "Minutes"
    minutes.Value = 0

    plr.CharacterAdded:Connect(function(char)
        local hum = char:WaitForChild("Humanoid")
        wait()
        char:MoveTo(checkpoints[stage.Value].Position)

        hum.Touched:Connect(function(hit)
            if hit.Parent == checkpoints then
                if tonumber(hit.Name) == stage.Value + 1 then
                    stage.Value = stage.Value + 1
                end
            end
        end)
    end)

    --load stage
    local success, result = loop(function()
        return pcall(stageDataStore.GetAsync, stageDataStore, plr.UserId)
    end, 3)
    loaded(plr, stage, success, result)

    --load minutes
    local success, result = loop(function()
        return pcall(minutesDataStore.GetAsync, minutesDataStore, plr.UserId)
    end, 3)
    loaded(plr, minutes, success, result)

    coroutine.resume(coroutine.create(function()
        while true do
            wait(60)
            minutes.Value = minutes.Value + 1
        end
    end))
end)

Players.PlayerRemoving:Connect(function(plr)
    -- save stage
    local success, result = loop(function()
        return pcall(stageDataStore.SetAsync, stageDataStore, plr.UserId, plr.leaderstats.Stage.Value)
    end, 3)
    saved(plr, success, result)

    -- save minutes
    local success, result = loop(function()
        return pcall(minutesDataStore.SetAsync, minutesDataStore, plr.UserId, plr.leaderstats.Minutes.Value)
    end, 3)
    saved(plr, success, result)
end)

remoteEvent.OnServerEvent:Connect(function(plr)
    if plr and plr.Character and plr.Character:FindFirstChild("Humanoid") then
        local humanoid = plr.Character.Humanoid
        plr.leaderstats.Stage.Value = 0
        humanoid.Health = 0
    end
end)

MarketplaceService.ProcessReceipt = function(info)
    local plr = Players:GetPlayerByUserId(info.PlayerId)

    local skips = {
        [106323481] = 1,
        [productId] = stagesToSkip
    }

    if skips[info.ProductId] then
        plr.leaderstats.Stage.Value += skips[info.ProductId]
        plr:LoadCharacter()
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end
end
0
Hi! Thanks a lot! I just have a small problem, I showed the wrong script for the leaderboard stage, here is the correct one: https://pastebin.com/8f4TB884 Thanks in advance! wDaerkfeX 37 — 1y
1
I've updated the script. It should handle both minutes and stage loading and saving. 9mze 193 — 1y
0
Hey! Thannks you for your answer, I have now an error in outpout: Stage is not a valid member of Folder "Players.wDaerkfeX.leaderstats" Thank you in advance! wDaerkfeX 37 — 1y
Ad
Log in to vote
1
Answered by 1 year ago

I think the problem is that you are making two separate leaderstats folders and Roblox is only using one of them.

I recommend putting it all in the same script like this:

local checkpoints = workspace:WaitForChild("Checkpoints droite")
local remoteEvent = game.ReplicatedStorage:WaitForChild("ResetStatEvent") 
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")

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

    local stage = Instance.new("IntValue")
    stage.Name = "Stage"
    stage.Value = 0
    stage.Parent = leaderstats

    local Minutes = Instance.new("IntValue")
    Minutes.Name = "Minutes"
    Minutes.Value = 0
    Minutes.Parent = Leaderstats

    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        Minutes.Value = Data
    end

    coroutine.resume(coroutine.create(function()
        while true do
            wait(60)
            Minutes.Value = Minutes.Value + 1
        end
    end))

    player.CharacterAdded:Connect(function(char)
        local hum = char:WaitForChild("Humanoid")
        wait()
        char:MoveTo(checkpoints[stage.Value].Position)

        hum.Touched:Connect(function(hit)
            if hit.Parent == checkpoints then
                if tonumber(hit.Name) == stage.Value + 1 then
                    stage.Value = stage.Value + 1
                end
            end
        end)
    end)
end)

remoteEvent.OnServerEvent:Connect(function(plr)
    local char = plr:WaitForChild("Character") or plr.CharacterAdded:Wait()
    if plr and char and char:FindFirstChild("Humanoid") then
        local humanoid = char:WaitForChild("Humanoid") or char:FindFirstChild("Humanoid") or nil
        plr.leaderstats.Stage.Value = 0
        humanoid.Health = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.leaderstats.Minutes.Value)
end)

I added some extra stuff in there to ensure no errors and to make the script for clean. Hope this helps!

0
Hey ty a lot for your answer, I have an error with the checkpoints, now they dont work, theres the output errors:Players.wDaerkfeX.PlayerGui.CharacterToStage:32: attempt to index nil with 'Position' and the other error is:Stage is not a valid member of Folder "Players.wDaerkfeX.leaderstats" line 7. Ty in advance for our help! have a nice day! wDaerkfeX 37 — 1y

Answer this question