Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Died event not firing on first death?

Asked by 8 years ago

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.

0
you never defined player theCJarmy7 1293 — 8y
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. FierceByte 25 — 8y
0
What does transport do? User#6546 35 — 8y
0
Lol. koolkid8099 705 — 8y
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. FierceByte 25 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

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.

Ad

Answer this question