✓ Accepted answer
From what you asked I think you are trying to say that you want them to start from 0 each time they join but still have their overall kills for each time they have played be saved somewhere. If you are just looking for a general datastore thats fine as well this will fith both needs.
Create a folder in ReplicatedStorage called PlayerData, update whatever pvp scripts you are using to update the Kills and Deaths inside that folder alongside your current leaderstats via the hierarchy of:
game.Players = game.Players.LocalPlayer
game:GetService("ReplicatedStorage").PlayerData:WaitForChild(player.name).Stats
You can then have a place in game or on a gui to display their total career stats by pulling the values from the folder referenced above. The main script below simply goes in a script in the ServerScriptService.
I added a cash line if you have a money system you would like to save as well but you can simply remove it if it is not needed as well as copying those lines for more stats you wish to create.
Don't forget to change the key on the first line as well.
local DS = game:GetService("DataStoreService"):GetDataStore("PUTRANDOMKEYHERE")
local PlayerData = game:GetService("ReplicatedStorage"):WaitForChild("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local PlayerFolder = Instance.new("Folder", PlayerData)
PlayerFolder.Name = player.name
local Stats = Instance.new("Folder", PlayerFolder)
Stats.Name = "Stats"
local Kills = Instance.new("IntValue", Stats)
Kills.Name = "Kills"
local Deaths = Instance.new("IntValue", Stats)
Deaths.Name = "Deaths"
local Cash = Instance.new("IntValue", Stats)
Cash.Name = "Cash"
local SavedData = DS:GetAsync(player.userId)
if SavedData then
for i,v in pairs(SavedData) do
if 1 % 2 == 0 then
PlayerData[player.Name].Stats:FindFirstChild(SavedData[i - 1]).Value = v
end
end
end
end)
local function SaveValues(player)
local Data = {}
for i,v in pairs(PlayerData[player.name].Stats:GetChildren()) do
Data[#Data + 1] = v.Name
Data[#Data + 1] = v.Value
end
DS:SetAsync(player.userId, Data)
end
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
SaveValues(player)
end
end)
game.Players.PlayerRemoving:connect(function(player)
SaveValues(player)
end)
2 comments
Did you check studio access to api services? (under configure this game)
Yea, the script works fine for what it does but I need to change the script itself