So basically I wanna update the player’s TOTAL kills with the kills they achieve on the current round, so it would be something like totalKills += kills… How would I do something like this with datastores? The main issue I am having is the first time the player joins I’m guessing the total kills won’t be initialised to 0 so I wouldn’t be able to do something like that?
I was thinking about doing something like this, however I am not sure if it would work
local players = game:GetService("Players")
local dss = game:GetService("DataStoreService")
local function initialiseDSS(plr)
local dataStore = dss:GetDataStore("dataStore")
if dataStore.getAsync(plr.UserId.."TotalKills") == nil then
dataStore.setAsync(plr.UserId.."TotalKills",0)
end
end
players.PlayerAdded:Connect(function(player)
initialiseDSS(player)
end)
So that’s what would happen when a first-time player joins, then when he leaves it would save with the totalKills += kills type thing
1 Like
Can’t you just add the player’s kills that round to their total kills? Also, I have made a DataStore script for you:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("dataStore")
game.Players.PlayerAdded:Connect(function(Player) --Player joins the game
local leaderstats = Instance.new("Folder") -- create leaderstats folder
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local TotalKills = Instance.new("IntValue") -- create Kills value
TotalKills.Name = "Total Kills"
TotalKills.Parent = leaderstats
local success, errorMsg
success, errorMsg = pcall(function()
data = myDataStore:GetAsync(Player.UserId) -- Attempt to load data
end)
if success then --if it worked
if data ~= nil then -- if player has joined before
TotalKills.Value = data.TKills
else -- if player is new
TotalKills.Value = 0
end
print("Loaded "..Player.UserId)
else -- if it didn't work
print(errorMsg)
TotalKills.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local success, Error
local DataToSave = { -- The data that will be saved
TKills = Player.leaderstats["Total Kills"].Value;
}
success, Error = pcall(function()
myDataStore:SetAsync(Player.UserId, DataToSave) -- Attempt to save data
end)
if success then -- if the data was saved
print("Saved "..Player.UserId)
else -- if the data wasn't saved
print(Error)
end
end)
3 Likes
Yes, the main problem was when they first join I was unsure how to deal with it being uninitialised, but I see you have covered that in the script!! Thank you so much! 
2 Likes