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)