Need help with resetting all the times on my leaderboard!

Hey y’all! I just made a post right before this one, but I wanted to keep the topics separate so it wouldn’t be more confusing than they already were. I just wanted to know how I could completely wipe my leaderboard free of times because I let a few friends test if the leaderboard worked and it does flawlessly. I was also informed about a plugin called datastore editor, I bought it, watched a handful of tutorials and showcases on YouTube about it but I’m clueless because I’m a beginner scripter! Here’s the script that handles the leaderboard itself and displays all players’ names and top times:


local DataStoreService = game:GetService("DataStoreService")

-- [ LOCALS ] --

local DataVer = "DataTimer" -- Data Store Name

local Store = DataStoreService:GetOrderedDataStore(DataVer.."LeaderBoardData")

local RationalDigits = 3 -- How many numbers show.

-- [ FUNCTIONS ] --

function GetLBData()

	local success, pages = pcall(function()
		return Store:GetSortedAsync(true, 10)
	end)

	if success and pages then
		local data = pages:GetCurrentPage()
		local a = 0
		for _, entry in pairs(data) do
			a = a + 1
			if a == 1 then

			end
			local val = entry.value / 1000
			local Addons={}
			for t=1,RationalDigits do
				local i = t-1
				local integer,predecimal = math.modf(val*(10^i))
				local decimal = predecimal/(10^i)
				if decimal == 0 then
					Addons[t] = "0"
				end

			end
			local NewText = tostring(val)
			for i,v in pairs(Addons) do
				NewText = NewText..v
			end
			script.Parent.SurfaceGui["A" .. tostring(a)].PlrName.Text = entry.key .. ": " .. NewText
		end
	end
end

GetLBData()

while wait(60) do
	pcall(function()
		GetLBData()
	end)
end

Oh, there’s no need to buy a plugin. I got a data store editing plugin before it was paid, and I think I wouldn’t have gotten it if it were paid from the start.

I can’t explain the plugin you have, but I do have some code that can wipe a data store. More info in this thread:

Alternatively, you can change the name of the store, but it might accumulate in your data store editor plugin.

1 Like

Waddup Ant, nice to see you again man! And oh God, where do I put that snippet of code? I read the other thread and it says “you can’t get these datastores back ever!” Is that good or bad? Like will it delete only the data on the leaderboard that I’m targeting or all datastores in the game?

Sorry for the late response. It will delete the keys inside the data store you specify. The snippet of code can actually be treated as a whole server script, but you must call the “clearData” function with the data store you specify. A data store is like when you do DataStoreService:GetDataStore("yourdatastore").

1 Like

All good mane we’re all busy sometimes! So it’ll remove the leaderboard data stuff for me? Will it change my whole script and mess up my game? Where do I add it in? Do I create a whole new server script or do I just add it in the script I originally posted?

You’re on the right track! Yes, you create a new server script temporarily. The process might take a while depending on the amount of entries. You just run the game and wait for the data to be deleted.

So, you’d paste the code into a new script and you’d want to do

clearData(game:GetService("DataStoreService"):GetOrderedDataStore("DataTimerLeaderBoardData"))

However, I might have to change a few things since I realize it’s an ordered data store and not a regular one. Ordered data stores are better for leaderboards, but they work a little bit differently. It should work fine though.

1 Like

PRECIATE YOU ANT! I’ll work on it first thing in the morning!:sunglasses::+1:t4:

Alright Ant this is what I have so far. I’m kind of hesitant on pressing publish right now because you said that I can’t recover sensitive information and I’m afraid that I might get rid of something that I didn’t mean to touch. Although you did say specify the correct datastore I’m afraid to touch publish without y’all on here letting me know if it’s correct or not! Where should I specify the datastore in this script, the parenthesis? And another question, does this act as like a reset progress button? What does it do?

function clearData(datastore)

	local listSuccess, pages = pcall(function()
		return datastore:ListKeysAsync()
	end)
	if listSuccess then
		local keys = {}
		local pagenum = 0
		while true do
			--print(pagenum)
			local items = pages:GetCurrentPage()

			for key, info in pairs(items) do
				table.insert(keys, info.KeyName)
			end
			if pages.IsFinished then break end
			local s,e = pcall(pages.AdvanceToNextPageAsync, pages)
			--if not s then warn("Deletion pages stopped, moving on.") break end
			if s then
				pagenum+=1
			else
				print("abort!")
				break
			end
			task.wait(1)
		end

		for _, key in pairs(keys) do
			local s, verpages = pcall(function()
				--print("Getting versions")
				return datastore:ListVersionsAsync(key, nil, nil, DateTime.now().UnixTimestampMillis - 2592000000)
			end)
			if s then
				while true do
					local items = verpages:GetCurrentPage()

					for dkey, info in pairs(items) do
						--local cts = tostring(info.CreatedTime)
						--local realCreationTime = tonumber(cts:sub(1, cts:len()-3))
						local realCreationTime = info.CreatedTime / 1000
						--print("Key:", dkey, "; Version:", info.Version, "; Created:", os.date("%c", realCreationTime), "; Deleted:", info.IsDeleted)
						if not info.IsDeleted and realCreationTime < os.time()-2592000 then
							print(key, datastore:GetAsync(key))
							warn("The key will be deleted.")
							datastore:RemoveVersionAsync(key, info.Version)
						end
					end
					if verpages.IsFinished then break end
					verpages:AdvanceToNextPageAsync()
				end
				task.wait(2)
			else
				--print(verpages)
				if verpages:lower():match("later") then
					print("Trying again later")
					task.wait(15)
				end
			end
		end
	else
		print(pages)
	end
end

clearData(game:GetService("DataStoreService"):GetOrderedDataStore("DataTimerLeaderBoardData"))

I’ll clarify a few things for you. The code looks correct and writes to the correct DataStore. This code actually does not require publishing, as it will delete data in Studio if you have allowed access to API services from within Studio. Don’t leave the script enabled if you publish because in the published version it will delete the data while people are playing, and you’re probably only going to use this occasionally.

Another thing to clarify is a question you’ve not asked yet, which is “how do I know when it is done?” You can find out when it is done by putting a line with print("done") after the line that starts with clearData. Whenever the output shows “done”, it’s finished.

You’ll need to keep the game running during the entirety of this process.

Anyways, there is one thing to note about the code snippet: it deletes data after 30 days to comply with GDPR, but it won’t delete data that is not older than 30 days. I’ve modified the code to suit your needs:

function clearData(datastore)

	local listSuccess, pages = pcall(function()
		return datastore:ListKeysAsync()
	end)
	if listSuccess then
		local keys = {}
		local pagenum = 0
		while true do
			--print(pagenum)
			local items = pages:GetCurrentPage()

			for key, info in pairs(items) do
				table.insert(keys, info.KeyName)
			end
			if pages.IsFinished then break end
			local s,e = pcall(pages.AdvanceToNextPageAsync, pages)
			--if not s then warn("Deletion pages stopped, moving on.") break end
			if s then
				pagenum+=1
			else
				print("abort!")
				break
			end
			task.wait(1)
		end

		for _, key in pairs(keys) do
			local s, verpages = pcall(function()
				--print("Getting versions")
				return datastore:ListVersionsAsync(key, nil, nil, 0)--DateTime.now().UnixTimestampMillis - 2592000000)
			end)
			if s then
				while true do
					local items = verpages:GetCurrentPage()

					for dkey, info in pairs(items) do
						--local cts = tostring(info.CreatedTime)
						--local realCreationTime = tonumber(cts:sub(1, cts:len()-3))
						--local realCreationTime = info.CreatedTime / 1000
						print("Key:", dkey, "; Version:", info.Version, "; Created:", os.date("%c", realCreationTime), "; Deleted:", info.IsDeleted)
						if not info.IsDeleted --[[and realCreationTime < os.time()-2592000]] then
							--print(key, datastore:GetAsync(key))
							warn("The key '"..key.."' will be deleted.")
							datastore:RemoveVersionAsync(key, info.Version)
						end
					end
					if verpages.IsFinished then break end
					verpages:AdvanceToNextPageAsync()
				end
				task.wait(2)
			else
				--print(verpages)
				if verpages:lower():match("later") then
					print("Trying again later")
					task.wait(15)
				end
			end
		end
	else
		print(pages)
	end
end

clearData(game:GetService("DataStoreService"):GetOrderedDataStore("DataTimerLeaderBoardData"))
print("done")

In the code above, it includes the mentioned fixes and improvements I’ve suggested, as well as some more print statements.

To put it simply, what it does is it will loop through all the data store pages, and from the pages, find the data store key pages, and from there, loop through the keys, detect if they’re deleted, and if they are not deleted, it will delete them. However, due to the limitations of DataStores, it cannot delete the metadata associated with the key.

1 Like

Appreciate your patience mane, a few other people on here have gotten attitudes with me about needing assistance and being new. And API services has been on ever since I started making the game so that’s good! So, I just put the script in the serverscriptservice, join the game then let it marinate for a little while till it finishes? And when it wipes the leaderboard, I can just disable the script in the properties tab till I need it again? THAT SOUNDS AWESOME! :sunglasses: :100:

Yeah, you got it. Good luck with your leaderboard!

1 Like

Thank you and preciate the help mane!

Also is this bad. It says unknown global “realCreationTime” for this line!

	print("Key:", dkey, "; Version:", info.Version, "; Created:", os.date("%c", realCreationTime), "; Deleted:", info.IsDeleted)

Oops, that was my bad.

function clearData(datastore)

	local listSuccess, pages = pcall(function()
		return datastore:ListKeysAsync()
	end)
	if listSuccess then
		local keys = {}
		local pagenum = 0
		while true do
			--print(pagenum)
			local items = pages:GetCurrentPage()

			for key, info in pairs(items) do
				table.insert(keys, info.KeyName)
			end
			if pages.IsFinished then break end
			local s,e = pcall(pages.AdvanceToNextPageAsync, pages)
			--if not s then warn("Deletion pages stopped, moving on.") break end
			if s then
				pagenum+=1
			else
				print("abort!")
				break
			end
			task.wait(1)
		end

		for _, key in pairs(keys) do
			local s, verpages = pcall(function()
				--print("Getting versions")
				return datastore:ListVersionsAsync(key, nil, nil, 0)--DateTime.now().UnixTimestampMillis - 2592000000)
			end)
			if s then
				while true do
					local items = verpages:GetCurrentPage()

					for dkey, info in pairs(items) do
						--local cts = tostring(info.CreatedTime)
						--local realCreationTime = tonumber(cts:sub(1, cts:len()-3))
						--local realCreationTime = info.CreatedTime / 1000
						print("Key:", dkey, "; Version:", info.Version, "; Deleted:", info.IsDeleted)
						if not info.IsDeleted --[[and realCreationTime < os.time()-2592000]] then
							--print(key, datastore:GetAsync(key))
							warn("The key '"..key.."' will be deleted.")
							datastore:RemoveVersionAsync(key, info.Version)
						end
					end
					if verpages.IsFinished then break end
					verpages:AdvanceToNextPageAsync()
				end
				task.wait(2)
			else
				--print(verpages)
				if verpages:lower():match("later") then
					print("Trying again later")
					task.wait(15)
				end
			end
		end
	else
		print(pages)
	end
end

clearData(game:GetService("DataStoreService"):GetOrderedDataStore("DataTimerLeaderBoardData"))
print("done")

This one removes that variable.

1 Like

Ima sleep and ima add this here first thing in the morning! But you said it doesn’t delete data that isn’t 30 days old? Dang that means I gotta wait a few more weeks to actually use the script then but its fine! Have a good night man :sunglasses: :+1:t4:

I fixed that in the code for you in the one I posted. No need to worry.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.