Humanoid.Died racing conditions?

Greetings!

I noticed in my code that Humanoid.Died only fires after the Character has been killed, LoadCharacter()'d or resets. I suspect this is due to racing conditions that are present in my script, and I’m just not seeing them. See here:

function newPlayer(addedPlayer)

	local function addedCharacter(addedCharacter)
		local function characterDied()
			addedPlayer.leaderstats.Deaths.Value += 1
		end

		if addedPlayer.TeamColor == game.ReplicatedStorage.gameData.groupTeam.Value or addedPlayer.TeamColor == BrickColor.new("Bright yellow") or addedPlayer.TeamColor == BrickColor.new("White") then
			if addedCharacter:FindFirstChild("Humanoid") then
				-- Need to unequip to prevent cheeky wads from beating the code
				addedCharacter.Humanoid:UnequipTools()
				-- Some weapons contain scripts for rendering on the client. This tries to keep that intact
				for _, object in pairs(addedPlayer.Backpack:GetChildren()) do
					if object:IsA("Tool") or object:IsA("ScreenGui") then
						object:Destroy()
					end
				end
			end
		else
			-- Verify that the mode isn't SIM. If it is, the cheeky wads may have slipped a weapon in somehow.
			if game.ReplicatedStorage.gameData.modeName.Value == "SIM" then
				addedCharacter.Humanoid:UnequipTools()
				-- Is ok in this instance because no one should have weapons on this mode anyway.
				addedPlayer.Backpack:ClearAllChildren()
			end
		end

		addedCharacter.Humanoid.Died:Connect(characterDied)
	end

	for _, player in pairs(game.Players:GetPlayers()) do
		if player ~= addedPlayer then
			game.ReplicatedStorage.Triggers.chatMessage:FireClient(player, addedPlayer:GetRoleInGroup(game.ReplicatedStorage.gameData.groupTeamId.Value).." "..addedPlayer.Name.." has joined.")
		end
	end

	local playerFolder = Instance.new("Folder")
	playerFolder.Name = addedPlayer.UserId

	local playerData = dataHandler.GetProfile(addedPlayer)

	local chatToggle = Instance.new("StringValue")
	chatToggle.Name = "chatToggle"
	chatToggle.Value = playerData.profile.settings.controls.chatToggle
	chatToggle.Parent = playerFolder

	local hatToggle = Instance.new("StringValue")
	hatToggle.Name = "hatToggle"
	hatToggle.Value = "Equals"
	hatToggle.Parent = playerFolder

	playerFolder.Parent = game.ReplicatedStorage.playerData

	local leaderStats = Instance.new("Folder")
	leaderStats.Name = "leaderstats"

	local killsValue = Instance.new("NumberValue")
	killsValue.Name = "Kills"
	
	local deathsValue = Instance.new("NumberValue")
	deathsValue.Name = "Deaths"

	local damageValue = Instance.new("NumberValue")
	damageValue.Name = "Damage"

	killsValue.Parent = leaderStats
	deathsValue.Parent = leaderStats
	damageValue.Parent = leaderStats

	leaderStats.Parent = addedPlayer

	for _, group in pairs(game:GetService("GroupService"):GetGroupsAsync(addedPlayer.UserId)) do
		if group.Id == game.ReplicatedStorage.gameData.groupTeamId.Value then
			addedPlayer.TeamColor = game.ReplicatedStorage.gameData.groupTeam.Value
		end
	end

	addedPlayer.CharacterAdded:Connect(addedCharacter)
	addedPlayer.CharacterAppearanceLoaded:Connect(updateAppearance)
end

game.Players.PlayerAdded:Connect(newPlayer)

Could it be because I have my addedPlayer.CharacterAdded located near the bottom? Not sure, please help!

This is definetly because the character has already loaded by the time it connects the function, try putting it on the top of the playeradded script.