Data doesn't load correctly

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.

You’re not actually setting the value of the IntValue when you load the data. Instead of doing Deaths = Data change it to Deaths.Value = Data

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.