Humanoid is not a valid member of player

Hello everyone! I’m trying to make a script that shows a ScreenGui when the player dies, but for some reason, when I try to access the Humanoid, to check if the player dies, it says that Humanoid is not a valid member of the Player…
Here’s the script:

--MainVars
local humanoid = game.Players.LocalPlayer.Character.Humanoid

--Code

Error:
22:11:16.758 Humanoid is not a valid member of Model "Workspace.quebrado" - Client - DIED!:2
The LocalScript is placed at the PlayerGui, inside the ScreenGui

2 Likes

You should check if there’s a character before trying to access the Humanoid

local player = game.Players.LocalPlayer

if player.Character then
      --alive
else
      --dead
end
2 Likes
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
2 Likes
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local gui = script:FindFirstAncestorWhichIsA("ScreenGui")

humanoid.Died:Connect(function()
	gui.Enabled = true
end)

and for the Gui functionality.

3 Likes

use

local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

-- your code here
2 Likes

The problem is that the character hasn’t yet loaded by the time this line of code executes.

2 Likes

What’s happening is that the script is running before the character loads. A solution to this is you use Player.CharacterAdded:Wait() which will pause the script until the character is added like this:

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait() --Will wait for the character to be added if it's not found immediately
local humanoid = char:WatiForChild("Humanoid")

Try This:

local ScreenGui = --Location of the ScreenGui

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
    --Code to perform when the Player dies
    ScreenGui.Enabled = true
end)

In fact, you should use a serverside which you can use a playeradded and characteradded

Players.PlayerAdded (roblox.com)
Player.CharacterAdded (roblox.com)

1 Like

Try this:

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid") or player.Character.Humanoid
print(humanoid.Name) -- To see if its humanoid

Server Script:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
local PlayerGui = Player:WaitForChild("PlayerGui")
local ScreenGui = PlayerGui:WaitForChild("ScreenGui")
    Player.CharacterAdded:Connect(function(Character)
        local Humanoid = Character:WaitForChild("Humanoid")
        Humanoid.Died:Connect(function(Died)
           ScreenGui.Enabled = true
        end)
    end)
end)

Local Script:

local ScreenGui = script.Parent --Location of ScreenGui

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function(Died)
    ScreenGui.Enabled = true
end)

Try this.

--MainVars
local Players = game:GetService(“Players”)
local humanoid = Players.LocalPlayer.Character:WaitForChild(“Humanoid”)

--Code

heyy why you trying to get humanoid from player
try this code

local humanoid = game.Players.LocalPlayer.Character.Humanoid