Deaths IntValue not on the leaderstats list

I’ve been having an issue for 3 days, thought it was fixed yesterday apparently not. Anyways I have an issue where the Deaths IntValue won’t attach to the playerlist. It decides to work some times and other times not, anyone know a solution? Thanks for your time, will provide you with more information if needed.

leaderstats script:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("SaveData")

local function SavePlayerData(player)
	
	local success, errormsg = pcall(function()
		local SaveData = {}
		for i, stats in pairs(player.leaderstats:GetChildren()) do
			SaveData[stats.Name] = stats.Value
		end	
		SaveDataStore:SetAsync(player.UserId, SaveData)
	end)
	
	if not success then 
		return errormsg
	end	
end

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value = 0
	level.Parent = leaderstats
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Value = 0
	XP.Parent = leaderstats
	
    local Deaths = Instance.new("IntValue", leaderstats)
    Deaths.Name = "Deaths"
    Deaths.Value = 0
	
local Data = SaveDataStore:GetAsync(player.UserId)
	
	if Data then
		for i, stats in pairs(leaderstats:GetChildren()) do
			stats.Value = Data[stats.Name]
		end		
	else		
		print(player.Name .. " has no data.")			
	end
	
	local expToLevelUp
	local expForPreviousLevel = 0
	
	player.CharacterAdded:Connect(function(char)
      char.Humanoid.Died:Connect(function()
	     Deaths.Value = Deaths.Value + 1
      end)   
	end)
	
	while wait() do
		
		local levelBar = player.PlayerGui:WaitForChild("LevelBar")	
		if level.Value < 1 then 
			
			expToLevelUp = 100 
		else
			expToLevelUp = math.floor(level.Value ^ 1.3) * 200 + math.floor(level.Value ^ 4)
		end
		
		if XP.Value >= expToLevelUp then
			level.Value = level.Value + 1	
		end
		
		expForPreviousLevel = math.floor((level.Value - 1) ^ 1.3) * 200 + math.floor((level.Value - 1) ^ 4)
		
		local expDifference = expToLevelUp - expForPreviousLevel
		local expDifference2 = XP.Value - expForPreviousLevel
			
		levelBar.Bar:TweenSize(UDim2.new(levelBar.BarBackground.Size.X.Scale * (expDifference2 / expDifference), 0, levelBar.BarBackground.Size.Y.Scale, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.001)
		levelBar.Experience.Text = expDifference2 .. "/" .. expDifference
		levelBar.Level.Text = "Level: " .. level.Value
		
		XP.Value = XP.Value + 1
	
	end
end)
	

Players.PlayerRemoving:Connect(function(player)
	
	local errormsg = SavePlayerData(player)
	
	if errormsg then	
		warn(errormsg)		
	end
end)

game:BindToClose(function()
	
	for i, player in pairs(Players:GetPlayers()) do	
		
		local errormsg = SavePlayerData(player)
		if errormsg then
			warn(errormsg)
		end
	end
	wait(2)	
end)

image0

1 Like

Why don’t you just do

local Deaths = Instance.new("IntValue")
Deaths.Name  = "Deaths"
Deaths.Value = 0
Deaths.Parent = leaderstats
2 Likes