So, for a brief backround... I tried making a DataStore that would save my total deaths. But it does not work?
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Deaths = Instance.new("IntValue", leaderstats)
Deaths.Name = "Deaths"
player.CharacterAdded:Connect(function(char)
Deaths.Value = Deaths.Value + 1
if Deaths.Value == 500 then
print("You leveled up!")
end
local PlayerUserId = "Player_"..player.UserId
-- Load Data
local Data
local sucess, errormessage = pcall(function()
Data = myDataStore:GetAsync(PlayerUserId)
end)
if sucess then
-- Setting our data equal to the current cash
Deaths.Value = Data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserId = "Player_"..player.UserId
-- Setting the data
local data = player.leaderstats.Deaths.Value
local sucess, errormessage = pcall(function()
myDataStore:SetAsync(PlayerUserId, data)
end)
if sucess then
print("Data succesfully saved")
else
print("Error occured whilst saving leaderstats...")
warn(errormessage)
end
end)
end)
And yes i have enabled API services

3 comments
You're not saving as the same key value. In loading your doing Player_1, and in the saving your doing 1. You need to use the same Player_playerUserId thing you have going.
Sorry, i dont seem to understand. I'm concating the PlayerUserId, like the following:
Could you use Lua Code Blocks in the formatting section of the question, so it's easier to read?