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

How can I fix a leaderboard issue?

Asked by 4 years ago

In my group game, I have 2 scripts for leaderboards and 1 for a point database. The first one is a point leaderboard and the other is a rank leaderboard for a group. When I use the points, the rank leaderboard doesn't work and vice versa. I am trying to debug but can't find the error.

Here is the AutoPoints script, in ServerScriptStorage:

amount = 1
timedelay = 180
currencyname = "Points"

while true do
    wait(timedelay)
    for i,v in pairs(game.Players:GetPlayers()) do
        if v:FindFirstChild("leaderstats") and v then
            v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount
        end
    end
end

Here is the PointDatabase script, in ServerScriptStorage:

--  NOTE FROM TROLLIANTBOT:
--  The points will not save if the game crashes.
--  The server can only take a number of requests per minute:
--  Getting Points: 60 + NumberOfPlayers X 10.
--  Saving Points: 60 + NumberOfPlayers X 10.

local DataStoreService = game:GetService("DataStoreService")

local PointSave = DataStoreService:GetDataStore("PointSave")

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

    local points = Instance.new("IntValue")
    points.Name = "Points"
    points.Parent = leaderstats

    local data
    local success, errormessage = pcall(function()
        data = PointSave:GetAsync(player.UserId)
    end)

    if success then
        points.Value = data
    else
        print("There was an error whilst loading your points.")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()
        PointSave:SetAsync(player.UserId,player.leaderstats.Points.Value)
    end)

        if success then
            print("Points saved!")
        else
            print("Points were not saved due to an error.")
            warn(errormessage)
        end

end)

And finally, here is the RankLeaderboard script, in ServerScriptStorage:

function added(player)
local ls = Instance.new("IntValue")
ls.Name = "leaderstats"

local role = Instance.new("StringValue")
role.Name = "Rank"

role.Value = player:GetRoleInGroup(5243394)
local Points = Instance.new("NumberValue")
Points.Name = "Points"
Points.Parent = ls

ls.Parent = player
role.Parent = ls
end
game.Players.PlayerAdded:connect(added)

If you have a fix, please answer. It would be really appreciated.

0
Have you tried combining these into 1 simultaneous working script, besides the while loop? killerbrenden 1537 — 4y
0
Oops, sorry about not adding player to my findfirstchild. Updated my answser moo1210 587 — 4y
0
Thanks! TrolliantBot 4 — 4y

1 answer

Log in to vote
0
Answered by
moo1210 587 Moderation Voter
4 years ago
Edited 4 years ago

This is because you are making two leaderboard objects, roblox's PlayerlistModule.lua module uses FindFirstChild so it will only detect one of the leaderstats. In one of the scripts remove ls.Name = "leaderstats" and ls.Parent = player then replace local ls = Instance.new("IntValue") with local ls = player:FindFirstChild("leaderstats")

0
Thanks for this! Sadly, the points get completely removed when I do this. Could you send me a script with the thing that you said in another answer? Thanks! TrolliantBot 4 — 4y
Ad

Answer this question