I'm working on a leaderboard and I have a function that fires when a player dies, however, it doesn't fire on the first death, it only happens on the second death of the player in the server. Here is my code in a ServerScript in ServerScriptService:
game.Players.PlayerAdded:connect(function(player)
local PauseButtonEvent = Instance.new("RemoteEvent")
PauseButtonEvent.Parent = game.ReplicatedStorage
PauseButtonEvent.Name = "PauseButtonEvent"
player.CharacterAdded:connect(function(character)
humanoid = character.Humanoid
print(player.Name .. " died")
transport(player)
PauseButtonEvent:FireClient(player)
humanoid.Died:connect(function()
deaths.Value = deaths.Value + 1
for i, child in pairs(humanoid:GetChildren()) do
if child:IsA("ObjectValue") and child.Value and child.Value:IsA("Player") then
local killer = child.Value
if killer:FindFirstChild("leaderstats") and killer:FindFirstChild("Kills") then
local killerKills = killer.leaderstats.Kills
killerKills.Value = killerKills.Value +1
end
return
end
end
end)
end)
end)
Also, if you see anything else wrong with my code beside what is causing the problem I explained, please tell me. Also, the game has filtering enabled.
Thank you in advance for any help.

5 comments
you never defined player
The code is in a function that fires when a player enters the game and the player is defined then. I didn't add in that whole function because there are other things in the function besides this. I'll add it anyways, though.
What does transport do?
Lol.
@eLunate It teleports the player, I guess I should have called it teleport. Everthing in the script works up until the died function on the first death, and then after that it all works except giving the killer a kill.