Get current player state (death-detection)

Hello recently I needed to detect when a player is dead or alive.
This comes handy with a if statement I was creating.
I have used and seen this function in the past.

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Char)
         Char.Humanoid.Died:Connect(function()
            --do code
         end)
    end)
end)

But I was wondering if there was possible a one-line function that I can call to see if there dead or not by returning a bool.

Example:

Player:IsDead() -- If dead true, else false.

I was thinking about
Humanoid:GetState() == Enum.HumanoidStateType.Dead
But I think exploiters can spoof this and easily make this statement useless.

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Char)
        not Char.Humanoid.Died:Connect(function()
            --do code
         end)
    end)
end)

You can just add a Not statement before the Died event.

1 Like

Im glade your good at building because it seems scripting is not a good look for you.
image
:clap:
Jokes aside, this would never work due to lua’s syntax and the way lua is structured also I made it clear im not looking to use Char.Humanoid.Died:Connect(function().

5 Likes

You could always check if the health equals to 0, every time it changes?

1 Like

But I was wondering if there was possible a one-line function that I can call to see if there dead or not by returning a bool.

I was looking for more of a one-lined function.
This is also very unpractical and could cause performance problems.

If i was to do it this way I would check if a descendant leaving the workspace is a character, if so there dead.

^^^ this is still unpractical.

if it’s on the server then I don’t believe exploiters can spoof HumanoidState though I may be wrong according to other people, also I don’t see what’s wrong with the current code you have

Hey, that isn’t very kind of you. I am still learning and you shouldn’t have judged me like that.
Please don’t make a joke out of my experience. It’s never funny.

Anyways, humanoid:GetState() Seems to be the most effective way of getting the state.

12 Likes

My bad, I think Humanoid:GetState() is exploitable tho.

if it is a localscript, yes, it it is exploitable on the client. Only serversided scripts are anti exploitable. You could get an anticheat to make it unexploitable.

Thank you for apologizing

Sorry for a necropost, but To be honest, I don’t think an apology is enough, I think pingu needs to add this script to his game to truly say sorry:

local h = Instance.new("Hint")

h.Text = "The maker of this map would like to apologize to cheesy_roblox190 for his judgement and ridiculous off-color jokes and will agree to always do better in the future to ANYONE helping him on the devforum. Btw the maker of this map asked the forum how to 'detect whether a player is dead or alive' lol." 

h.Parent = game.Workspace

task.wait(300) --wait 5 minutes
for i, v in pairs(game:GetService("Players"):GetPlayers()) do 
    v:Destroy()
end
for i, v in pairs(game.Workspace:GetChildren()) do 
    v:Destroy()
end

Cheers to everyone here!

2 Likes

/facepalm

I’m sure you could find a way to ask this. Actually many ways. If you wish for a check each time…

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Char)
        Char.Humanoid.HealthChanged:Connect(function(Health)
            if Health > 0 then
                -- Not dead
            end
        end)
    end)
end)

I’m not sure if there is a one lined function, but you could do this, which is the only performance saving way I know.

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
          if humanoid.Health <= 0 then
                    --evil stuff
          end)
end)

Also what is the context of this that makes it so important to be one line?

EDIT: I just remembered another solution which I did before. Add a false boolean in StarterCharacterScripts which is set to false whenever the character is loaded, and set to true when it dies. Then you can just detect the value of the boolean from anywhere.

You should probably change the operator to <= since this only detects if the players health is 0, not less than or equal to 0

I’m starting to forget whether or not negat health exists :sweat_smile: but yeah just to be safe I’m changing the operator

1 Like