Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Died event not firing on first death?

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

0

you never defined player

0

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.

0

What does transport do?

0

Lol.

0

@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.

1 answer

✓ Accepted answer

I believe this is to do with the character spawning before the function has fully completed. I've met this a few times when coding my own scripts, so I prefer to do something along these lines:

loaded = function(character)
	humanoid = character.Humanoid
        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

game.Players.PlayerAdded:connect(function(player)
    local PauseButtonEvent = Instance.new("RemoteEvent") --Not sure why you want to make a new one every time the player joins...
    PauseButtonEvent.Parent = game.ReplicatedStorage
    PauseButtonEvent.Name = "PauseButtonEvent"

   repeat wait() until player.Character

   loaded(player.Character)

    player.CharacterAdded:connect(function(character)
        loaded(character)
   end)
end)

So it waits until the character has first loaded before running the code.

This discussion is preserved as it was. New answers, comments, and votes are closed.