DataStore Is in complete chaos

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to achieve on fixing the leaderboard, And a donation game.

  1. What is the issue? Include screenshots / videos if possible!

There is a lot of issues, when I test on single player (Roblox studio, and Roblox) it works but when I test on Roblox studio the datastore is printing my name and the players in are Player1 and Player2 and Player3, and it fails to find a player Called my name, and the other issue is when I try playing in normal Roblox (not in studio) is I Brang 3 players, me, my alt, and my friend. me and my friend joined together, and everything was working. and I Brang my alt and my friend left and the entire script broke.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried fixing but the datastore is just too much for me

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

my script (It wasn’t made by me):

-- [ SETTINGS ] --

local statsName = "Donated" -- Your stats name
local maxItems = 100 -- Max number of items to be displayed on the leaderboard
local minValueDisplay = 0 -- Any numbers lower than this will be excluded
local maxValueDisplay = math.huge -- (10 ^ 15) Any numbers higher than this will be excluded
local abbreviateValue = true -- The displayed number gets abbreviated to make it "human readable"
local updateEvery = 15 -- (in seconds) How often the leaderboard has to update
local headingColor = Color3.fromRGB(25, 181, 254) -- The background color of the heading

-- [ END SETTINGS ] --




-- Don't edit if you don't know what you're doing --

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DataStore = DataStoreService:GetOrderedDataStore("GlobalLeaderboard_" .. statsName)
local Frame = script.Parent.Frame
local Contents = Frame.Contents
local Template = script.objTemplate

local COLORS = {
	Default = Color3.fromRGB(38, 50, 56),
	Gold = Color3.fromRGB(255, 215, 0),
	Silver = Color3.fromRGB(192, 192, 192),
	Bronze = Color3.fromRGB(205, 127, 50)
}
local ABBREVIATIONS = { "K", "M", "B", "T", "Qa", "Qi", "Sx"}


local function toHumanReadableNumber(num)
	if num < 1000 then
		return tostring(num)
	end
	
	local digits = math.floor(math.log10(num)) + 1
	local index = math.min(#ABBREVIATIONS, math.floor((digits - 1) / 3))
	local front = num / math.pow(10, index * 3)
	
	return string.format("%i%s+", front, ABBREVIATIONS[index])
end

local function getItems()
	local data = DataStore:GetSortedAsync(false, maxItems, minValueDisplay, maxValueDisplay)
	local topPage = data:GetCurrentPage()

	for position, v in ipairs(topPage) do
		local userId = v.key
		local value = v.value
		local username = "[Not Available]"
		local color = COLORS.Default
		local winners = game.ReplicatedStorage:WaitForChild("Bindables"):WaitForChild("WinnerEvent")
		
		Contents.Items.Nothing.Visible = #topPage == 0 and true or false
		
		local success, err = pcall(function()
			username = Players:GetNameFromUserIdAsync(userId)
		end)
		
		print(username)
		
		if position == 1 then
			color = COLORS.Gold
			winners:Fire(workspace:WaitForChild(username), 1)
		elseif position == 2 then
			color = COLORS.Silver
			winners:Fire(workspace:WaitForChild(username), 2)
		elseif position == 3 then
			color = COLORS.Bronze
			winners:Fire(workspace:WaitForChild(username), 3)
		end

		local item = Template:Clone()
		item.Name = username
		item.LayoutOrder = position
		item.Values.Number.TextColor3 = color
		item.Values.Number.Text = position
		item.Values.Username.Text = username
		item.Values.Value.Text = abbreviateValue and toHumanReadableNumber(value) or value
		item.Parent = Contents.Items
	end
end


script.Parent.Parent.Color = headingColor
Frame.Heading.ImageColor3 = headingColor
Frame.Heading.Bar.BackgroundColor3 = headingColor

while true do
	for _, player in pairs(Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")

		if not leaderstats then
			warn("Couldn't find leaderstats!")
			break
		end

		local statsValue = leaderstats:FindFirstChild(statsName)

		if not statsValue then
			warn("Couldn't find " .. statsName .. " in leaderstats!")
			break
		end

		pcall(function()
			DataStore:UpdateAsync(player.UserId, function()
				return tonumber(statsValue.Value)
			end)
		end)
	end

	for _, item in pairs(Contents.Items:GetChildren()) do
		if item:IsA("Frame") then
			item:Destroy()
		end
	end

	getItems()

	wait()
	Frame.Heading.Heading.Text = statsName .. " Leaderboard"
	Contents.GuideTopBar.Value.Text = statsName
	wait(updateEvery)
end

If you want some script just reply, any replies helps.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Part of the issue that I see is that you don’t have a delay in the first while true do loop. You need to delay it more than just using a wait(), especially when working with data stores because they server will run it full bore and overload the datastore queue. You should probably update the datastore every 2-5 minutes. Try this:

local counter = 0
while true do
	for _, player in pairs(Players:GetPlayers()) do
		local leaderstats = player:FindFirstChild("leaderstats")

		if not leaderstats then
			warn("Couldn't find leaderstats!")
			break
		end

		local statsValue = leaderstats:FindFirstChild(statsName)

		if not statsValue then
			warn("Couldn't find " .. statsName .. " in leaderstats!")
			break
		end

		if count % 1200 == 0 then
			pcall(function()
				DataStore:UpdateAsync(player.UserId, function()
					return tonumber(statsValue.Value)
				end)
			end)
		end
	end

	for _, item in pairs(Contents.Items:GetChildren()) do
		if item:IsA("Frame") then
			item:Destroy()
		end
	end

	getItems()

	wait(0.1)
	count += 1
	Frame.Heading.Heading.Text = statsName .. " Leaderboard"
	Contents.GuideTopBar.Value.Text = statsName
	wait(updateEvery)
end

This will update the leaderboard every 0.1 seconds (or 10 times a second), and it will save the leaderstats every 2 minutes (120 seconds / 0.1 = 1200).

And in Roblox Studio, the default players are Player1, Player2, Player3, etc… and they will have UserId -1, -2, -3, etc… respectively. That’s just the nature of the beast.

Part of the other issue is that when the player leaves, there needs to be a way to remove their data from the server’s memory, or else you will have a memory leak. Take a look at the Player.PlayerRemoving event.

it doesn’t work and I don’t have a play leaving event.

You really need a player leaving event otherwise the server will have a memory leak and that’s not good. What is it doing now?

I’m really looking at your script and the logic is…weird to say the least. I’m going to have to think about this one.

wait let me fix this (I don’t use notification)

It looks like it’s using the storage service as a semaphore? You only need to read the data in once and then update and save in periodic intervals. Then the script is destroying the leaderboard frame? And there’s no comments to explain any of the logic.

I know you didn’t write it. But, whoever wrote this needs to have something bad happen to them.

wait I think I know why its happing.

first of all, it checks for the players that recently played and it tries to find the character of it in the workspace…

I think I know how to fix this, but I think I need to clear all the data.

When the player leaves it just gets the character for it and clones it and put it into the ServerStorage, I think it will fix the problem?

and how do I clear the data that’s my new problem now.

The other thing that I noticed is that it’s pulling the player’s name by user id from the Roblox website…in a loop…with almost no throttling…and no caching. That’s not a good thing to do. If a bunch of servers start doing this, that other server could go down. Plus, that’s a very expensive call.

It’s obvious that this script deals with the leaderstats. Is this using the all time high for the leaderstats? Like a global leaderstat board of the top players? If so, that would explain a few things, and I would do it differently so that other things don’t get broken. I don’t see anything dealing with loading in a player’s character.

As for crashing, I’ve had Roblox Studio do that to me a few times.

As for clearing out the datastore, there was a script that was posted on the forums a few months ago that allows someone to erase all data in all datastores. I’ll see if I can find it.

Found it: Script that can remove all data stores in your data stores