Deaths aren't counting for leaderstats

yes when i died it said i had 830 deaths LOL.

Your Humanoid.Died() is inside the while wait() so it will keep connecting the function forever, essentially meaning it will stack another death every time that loop reoccurs

should i move it out of the while wait() ?

Yes, that should result in just 1 death added, instead of 830.

so it worked thank you! but now if you want to continue helping me you can because ive ran into a further problem! when i die the XP which is another thing in the leaderstats start working. otherwise if i dont die the XP doesn’t run. do u think i could put this somewhere else in my 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
    Deaths.Parent = leaderstats

	
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

	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
end

i copied and pasted it, just dunno where to put it where it wouldn’t affect other scripts.

I’m not exactly sure where to put it, and sorry if this is off topic but as a heads up what I’ve noticed is that you’re trying to change GUI components on the server. You will have to tween the UI with a localscript.

1 Like