Deaths aren't counting for leaderstats after 1 death

The title says it all, everything is in one script which is the leaderstats script. There are no errors relating to the deaths in the script. Thanks for your time and I 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
    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
	
		local char = player.Character or player.CharacterAdded:Wait()
        local humanoid = char:WaitForChild("Humanoid")

        humanoid.Died:Connect(function()
	        player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
	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)

Any errors in output?
Thirty Characters
Edit: nevermind didnt realise the line at the top

Quite sure you have to bind the function to the Died event on the new humanoid upon CharacterAdded.

1 Like

Could you show me an example of how I would go about doing that? Still in the learning phase for scripting.

game.Players.PlayerAdded:Connect(function(player)
         player.CharacterAdded:Connect(function(character)
                character.Humanoid.Died:Connect(function()
                end)
         end)
end)
Player.CharacterAdded:Connect(function(char)
      char.Humanoid.Died:Connect(function()
            —Increase deaths
      end)
end)

@RudraVII’s solution should fix your problem. However, note that you have to place that player.CharacterAdded before the while loop, or your function will never be connected.

Also, just a small observation, in your script you are parenting an instance to the same object twice for no reason:

local Deaths = Instance.new("IntValue", leaderstats) -- create an IntValue and parent it to leaderstats
...
Deaths.Parent = leaderstats -- parent the created IntValue to leaderstats

As you can see, this is redundant (just delete Instance.new 2nd argument or that last command).

Thanks, you. Just fixed that, also Player. is underlined in red. Will players. fix the problem?

change it to player and it should fix.

Your function’s argument is player (no capital letter), but if that was the problem it should be marked with a blue line. Red lines inform you that the script will not run properly, e.g. because of a syntax error.

In these cases, you can right click > press “Go to script error” and read what’s exactly causing the problem. Maybe it is just that you didn’t complete the statement yet.

Thanks! Also this error happened? 11:59:03.854 - ServerScriptService.leaderstats:107: Expected ‘end’ (to close ‘function’ at line 20), got

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()
      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)

you need an end) underneath char.Humanoid.Died

	player.CharacterAdded:Connect(function(char)
      char.Humanoid.Died:Connect(function()
      end)
      end)

It says it expected to close at line 54?

Is that the full script?
And you still also need the end) underneath Humanoid.Died

Nope that was just a segment. Here is the full 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()
      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)

do you still get the error after adding the end) under .Died()>?

Isn’t that already apart of the script?

	player.CharacterAdded:Connect(function(char)
      char.Humanoid.Died:Connect(function()
      end)
	end)

Is it in your script in studio?

Yup, its in serverscriptservice.