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
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
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)
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")
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)
local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid") or player.Character.Humanoid
print(humanoid.Name) -- To see if its humanoid
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)