How can I manually reset a datastore?

I am hosting an event across a few different weekends, and it will be a pvp event where the winner of each is week is the one with the most kills. Since the event is across multiple days, players scores will save to a datastore and be displayed on a scoreboard in the map.
After each week, I would like to manually reset everyones kills and deaths to zero, so they start fresh the next weekend.
After the weekend ends, the winners will be recorded and I wont need their scores anymore.

In short, I want players to keep their score during the days the game is open, and have it reset by the next week.

My datascore script isnt original, but I did add a ratio to the leaderstats:

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")

	local killsKey = player.UserId.."-Kills"
	local deathsKey = player.UserId.."-Deaths"
	local ratioKey = player.UserId.."-Ratio"

	local kills, deaths, ratio = 0, 0, 0

	local success, error = pcall(function()
		kills = dataStore:GetAsync(killsKey) or 0
		deaths = dataStore:GetAsync(deathsKey) or 0
		ratio = dataStore:GetAsync(ratioKey) or 0
	end)

	if success then
		leaderstats.Kills.Value = kills
		leaderstats.Deaths.Value = deaths
		leaderstats.Ratio.Value = ratio
	else
		warn("Failed to retrieve data: " .. error)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		local killsKey = player.UserId.."-Kills"
		local deathsKey = player.UserId.."-Deaths"
		local ratioKey = player.UserId.."-Ratio"

		local success, error = pcall(function()
			dataStore:SetAsync(killsKey, leaderstats.Kills.Value)
			dataStore:SetAsync(deathsKey, leaderstats.Deaths.Value)
			dataStore:SetAsync(ratioKey, leaderstats.Ratio.Value)
		end)

		if not success then
			warn("Failed to save data: " .. error)
		end
	end
end)
4 Likes

Change the datastore weekly (I’m lazy :joy:)

1 Like

Please explain how to do that as if you were speaking to a caveman :upside_down_face:

1 Like

If you change myDataStore to myDataStore1 for example

4 Likes

That seems to have worked, thank you!
I have been told we are limited on Datastores. Is there any way to delete them after the event is finished?

1 Like

Don’t worry about the limits. They don’t apply for this scenario (as far as I’m aware since I’m already on datastore 254 :joy:)

1 Like

Awesome! Thank you for your help!

1 Like

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