#Module Global Leaderstats not working (HELP ME)

1 messages · Page 1 of 1 (latest)

gilded helm
#

I've been making a leaderstats for my game so people can flex and i've been having a problem with it

Module

local module = {}

function module.GlobalLeaderstats(Model, ValName)
    local players = game:GetService("Players")
    
    local DataStoreService = game:GetService("DataStoreService")
    local LeaderstatOrdered = DataStoreService:GetOrderedDataStore("storage")
    
    local leaderboardModel = Model
    
    local slotTemplate = leaderboardModel:WaitForChild("LeaderboardSlot")
    local mainFrame = leaderboardModel.Panel.MainFrame
    local loadFrame = leaderboardModel.Panel:WaitForChild("LoadFrame")
    local scrollFrame = mainFrame:WaitForChild("ScrollingFrame")
    local gridLayout = scrollFrame:WaitForChild("UIGridLayout")
    
    local smallerFirst = true
    local numberToShow = 50
    local min = 0
    local max = 10e30
    
    local function updateLeadboard()
    
    
    --Enable Loading
        mainFrame.Enabled = false
        loadFrame.Enabled = true
        
        gridLayout.Parent = script
        scrollFrame:ClearAllChildren()
        
        ---Load Data On Global Leaderboard
        
        local pages = LeaderstatOrdered:GetSortedAsync(smallerFirst, numberToShow, min, max)
        local leaderboardData = pages:GetCurrentPage()
        
        for rank, plrData in pairs(leaderboardData) do
            
            print("working1")
            local userid = plrData.key
            local leaderValues = plrData.value
            local name = "[NotAvailable]"
            
            name = players:GetNameFromUserIdAsync(userid)
            
            local newSlot = slotTemplate:Clone()
            newSlot.Name = userid
            newSlot.LayoutOrder = rank
            newSlot.NameLabel.Text = name
            newSlot.RankLabel.Text = "#" .. rank
            newSlot.ScoreLabel.Text = leaderValues
            newSlot.Parent = scrollFrame
            
            print("working2")
            
            local rankColor = newSlot.RankLabel.TextColor3
            
            if rank == 1 then
                rankColor = Color3.fromRGB(255,187,28)
            elseif rank == 2 then
                rankColor = Color3.fromRGB(182,196,209)
            elseif rank == 3 then
                rankColor = Color3.fromRGB(170,71,32)    
            end
            
            newSlot.RankLabel.TextColor3 = rankColor
            
            print("working 3")
        end
        
        --disable Load Screen
        mainFrame.Enabled = true
        loadFrame.Enabled = false
        
        gridLayout.Parent = scrollFrame
    end
    
    updateLeadboard()
    print("working4")
    task.spawn(function()
        while task.wait(60) do
            updateLeadboard()
        end
    end)
    
end
return module
spring iglooBOT
#

studio** You are now Level 4! **studio

gilded helm
#

Leaderstats

local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("storage")

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

    local wins = Instance.new("IntValue")
    wins.Name = "Wins"
    wins.Value = 0
    wins.Parent = leaderstats
    
    local MultiplayerWins = Instance.new("IntValue")
    MultiplayerWins.Name = "MultiplayerWins"
    MultiplayerWins.Value = 0
    MultiplayerWins.Parent = leaderstats

    local TimeSpent = Instance.new("IntValue")
    TimeSpent.Name = "TimeSpent"
    TimeSpent.Value = 0
    TimeSpent.Parent = leaderstats
    -- Load data
    local success, data = pcall(function()
        return playerDataStore:GetAsync(player.UserId)
    end)

    if success and data then
        wins.Value = data.wins or wins.Value
        MultiplayerWins.Value = data.MultiplayerWins or MultiplayerWins.Value
        TimeSpent.Value = data.TimeSpent or TimeSpent.Value
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    -- Save data
    local success, error = pcall(function()
        playerDataStore:SetAsync(player.UserId, {
            wins = player.leaderstats.Wins.Value,
            MultiplayerWins = player.leaderstats.MultiplayerWins.Value,
            TimeSpent = player.leaderstats.TimeSpent.Value,
        })
    end)
    if not success then
        warn("Failed to save data for player " .. player.Name .. ": " .. error)
    end
end)

-- Increase Time every minute
while task.wait(60) do
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player and player:FindFirstChild("leaderstats") then
            local TimeSpent = player.leaderstats.TimeSpent
            if TimeSpent then
                TimeSpent.Value += 1
            end
        end
    end
end
#

requring script

local mod = require(game:GetService("ReplicatedStorage").ModulerScripts.GlobalLeaderStats)

mod.GlobalLeaderstats(workspace.Spawn.GlobalLeaderBoard.Wins, "Wins")