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

Why is the player's death not being detected?

Asked by 7 years ago
local player = game.Players.LocalPlayer
local char = player.Character
local storage = game:GetService("ReplicatedStorage")

char.Humanoid.Died:Connect(function()
    for i,v in pairs(storage.Contestants:GetChildren()) do
        if v.Name == player.Name then
            v.Dead.Value = true
        end
    end
end)

This script only works once, then never works again. It is a localscript in StarterGui and this happens in both studio and in-game. Help?

0
The reason it only works once is because once the player respawns a new character is created. You're only defining that one character once so every time you die it's getting that same characters humanoid (which is nil because it was destroyed after death). Use a playeradded event with a characteradded event inside of it checking when the humanoid dies. Hope this helps :) NewVoids 97 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Okay. There are a few things that I notice, in which should be changed. The biggest is that you are locally changing the BoolValue, which wouldn't be a good idea if your game was to go into FE. Assuming that this script is to always run, this is simply the way I would do it:

--A server script in workspace

local storage = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").Died:connect(function()
            for index, child in pairs(storage.Contestants:GetChildren()) do
                if child.Name == player.Name then
                    child.Dead.Value = true
                end
            end
        end)
    end)
end) -- Expect typos because I didn't test this in studio, just typed it out here

What I changed: - I made this a server sided script that I'd use for a FE game -Got rid of single lettered variables - Probably made a script that works

0
How would this work...when the player is added, the humanoid wouldn't be dead? thehybrid576 294 — 7y
0
^ Incorrect. This will bind the following to the character. Once the Humanoid dies, the .Died:Connect will work. I'm pretty sure this code works iamnoamesa 674 — 7y
Ad

Answer this question