Making a total time stats

Hello currently I am experiencing a problem when i have a text label I am trying to make it display an all time stats for an leadrstats called ‘Gold’ but when I try to do so I can’t achieve it so I could use help making a total stats so it stores the stats earns for Gold and Gems and then when I want to display on a text label I just display the Totalstats for Gold so for example TotalGold and then it shows me what have I got in the past so it adds on. Thanks :>.

2 Likes

Do you already have your gold stored in a DataStore?

3 Likes

Yes I have a leadrstats that save for gold and gems.

1 Like

The basis of what you need is GetSortedAsync. This can return the top values for an OrderedDataSstore. Then you can display it on a board in your game.

1 Like

Could you code it for me since i am not good at coding. And this is the leadrstats code but my actual leadrstats is Gold and Gems.
local DataStoreService = game:GetService(“DataStoreService”)
local playerDataStore = DataStoreService:GetDataStore(“PlayerDataStore”)

game.Players.PlayerAdded:Connect(function(player)
– Create leaderstats folder
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

-- Create a currency value called Dollars
local dollars = Instance.new("IntValue")
dollars.Name = "Dollars"
dollars.Value = 0 -- Default value
dollars.Parent = leaderstats

-- Load the player's saved data
local success, savedData = pcall(function()
    return playerDataStore:GetAsync(player.UserId)
end)

if success and savedData then
    dollars.Value = savedData
else
    print("No saved data for player:", player.Name)
end

end)

game.Players.PlayerRemoving:Connect(function(player)
– Save the player’s data when they leave
local leaderstats = player:FindFirstChild(“leaderstats”)
if leaderstats then
local dollars = leaderstats:FindFirstChild(“Dollars”)
if dollars then
local success, errorMessage = pcall(function()
playerDataStore:SetAsync(player.UserId, dollars.Value)
end)
if not success then
warn(“Failed to save data for player:”, player.Name, errorMessage)
end
end
end
end)

I just would need a script for the textlabels to display the amounts.

1 Like

I don’t know what it just did there but this is the script.

local DataStoreService = game:GetService(“DataStoreService”)
local playerDataStore = DataStoreService:GetDataStore(“PlayerDataStore”)

game.Players.PlayerAdded:Connect(function(player)
– Create leaderstats folder
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

-- Create a currency value called Dollars
local dollars = Instance.new("IntValue")
dollars.Name = "Dollars"
dollars.Value = 0 -- Default value
dollars.Parent = leaderstats

-- Load the player's saved data
local success, savedData = pcall(function()
    return playerDataStore:GetAsync(player.UserId)
end)

if success and savedData then
    dollars.Value = savedData
else
    print("No saved data for player:", player.Name)
end

end)

game.Players.PlayerRemoving:Connect(function(player)
– Save the player’s data when they leave
local leaderstats = player:FindFirstChild(“leaderstats”)
if leaderstats then
local dollars = leaderstats:FindFirstChild(“Dollars”)
if dollars then
local success, errorMessage = pcall(function()
playerDataStore:SetAsync(player.UserId, dollars.Value)
end)
if not success then
warn(“Failed to save data for player:”, player.Name, errorMessage)
end
end
end
end)

1 Like

You will need an OrderedDataStore not a regular one.

1 Like

Could you help me in doing it?

1 Like

There is a mostly complete example here, just substitute your stats for Points. Note that if they are currently in a normal DataStore you’ll have to copy them over.

1 Like

I will try. Tomorrow and I will get back to you.

1 Like