Basically, i have this pretty advanced data saving system for one of my customer, however my customer asked how i can reset stats for one player in case if we find out this user is exploiting.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = false
local success,errorm = pcall(function()
DataStore = DataStoreService:GetDataStore("MoneyFtMiles")
end)
game.Players.PlayerAdded:Connect(function(player)
-- Collect user data
local getsuccess,result = pcall(function()
return DataStore:GetAsync(player.UserId)
end)
-- The stats aren't gonna load! roblox might be down so stop right there
if not getsuccess and result then
warn("Load failed for Money and miles : "..result)
player:Kick("Something doesn't allow you're stats to properly load at the time. Please rejoin")
return
end
-- Player isn't in the datastore
if getsuccess and not result then
result = {
Money = 0,
Miles = 0
}
end
-- Predefine anti down variable
local group
-- If the group can be found without error Roblox is not down
local groupsuccess,grouperror = pcall(function()
group = player:IsInGroup(1)
end)
-- No need to keep it
group = nil
-- Handle potential downside
if not groupsuccess and grouperror then
warn("Roblox might be down : "..grouperror)
player:Kick("Roblox might be down at the moment, please join later to protect your progress.")
return
end
-- Create the stats
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local Money = Instance.new("NumberValue")
Money.Name = "Money"
Money.Value = result.Money or 0
Money.Parent = leaderstats
local Miles = Instance.new("NumberValue")
Miles.Name = "Miles"
Miles.Value = result.Miles or 0
Miles.Parent = leaderstats
end)
game.Players.PlayerRemoving:Connect(function(player)
if not player:FindFirstChild("leaderstats") then
return
end
local Money = player.leaderstats:FindFirstChild("Money")
local Miles = player.leaderstats:FindFirstChild("Miles")
if not Money or not Miles then
return
end
success,errorm = pcall(function()
DataStore:SetAsync(player.UserId,{
["Money"] = Money.Value;
["Miles"] = Miles.Value;
})
end)
if not success then
warn("SAVING FAILLURE : "..errorm)
end
end)