I want my data to save correctly. It is loading my data, but it is always 0. I haven’t found anything on the dev forum for my specific situation.
Here is my script
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Player
local Deaths = Instance.new("IntValue")
Deaths.Name = "Deaths"
Deaths.Parent = leaderstats
local Key = Player.UserId
local Data
local Success, Error = pcall(function()
Data = PlayerData:GetAsync(Key)
end)
if Success then
print("Success")
Deaths = Data
else
print("Couldn't load data")
Deaths = 0
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Key = Player.UserId
local Success, Error = pcall(function()
PlayerData:SetAsync(Key, Player:WaitForChild("leaderstats"):WaitForChild("Deaths").Value)
end)
if not Success then
warn(Error)
end
end)
It prints Success so I know it loads it in, but it is always 0 for the number of deaths and also I have API Services on.