Death gui wont appear when death

Hello, my death gui wont appear when a player dies. can someone help?

my code =

local DeathGui = game.StarterGui.DeathGui  ---death gui is main gui
local DeathFrame = game.StarterGui.DeathGui.DeathFrame ---- is frame
local Title = game.StarterGui.DeathGui.DeathFrame.Title ---- textlabel
local player = game.Players.Name --- finds name of player
local humanoid = game.Players:FindFirstChild(player).humanoid ---- the humanoid of the player

if humanoid.Health == 0 then 
	DeathGui.Enabled = true
end

thanks. im new so if this code is very basic its on me

1 Like

There is a few things you need to correct to get this script to work.

game.Players.Name is always “Players”, not the local player’s name.
You can get the local player’s name with game.Players.LocalPlayer.Name, but this is still not required to get the player’s humanoid. You can easily do game.Players.LocalPlayer.Character.Humanoid, but the character most likely won’t be there yet, unless this script is in StarterCharacterScripts.

You have the variable DeathGui but decide not to use it when making more variables. Try to stay organized and efficient!

The GUIs in StarterGui are not what the player is seeing. It is simply just a template that the player will get when they join the game. Their actual GUIs are in Player.PlayerGui.

So. When you make all these changes, you get this:

local player = game.Players.LocalPlayer
local ui = player.PlayerGui.DeathGui
local message = ui.DeathFrame
local title = message.Title
local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid

if humanoid.Health == 0 then 
	DeathGui.Enabled = true
end

Unfortunatly, this still won’t work.
When the script runs, the humanoid’s health is most likely 100. So DeathGui.Enabled = true never runs. It will never check the health again because you didn’t ask it to check multiple times.
You can fix this by using a combination of Humanoid.Died and player.CharacterAdded.

local player = game.Players.LocalPlayer
local ui = player.PlayerGui.DeathGui
local message = ui.DeathFrame
local title = message.Title
local player = game.Players.LocalPlayer

local function oncharacter(char)
    char.Humanoid.Died:Connect(function()
        ui.Enabled = true
    end)
end

if player.Character then oncharacter(player.Character) end
player.CharacterAdded:Connect(oncharacter)
1 Like

this is the final script? i tried it does not work
@bluebxrrybot

Why doesn’t it work? Any errors?

error = ServerScriptService.Script:2: attempt to index nil with ‘PlayerGui’

Put the code in a LocalScript, not a Script.

Use the code in a LocalScript, parented to StarterPlayerScripts. You’ve got a function bound to the CharacterAdded event, so it should work here.

StarterPlayerScripts - Character not guaranteed to exist. Executes once.
StarterCharacterScripts - Character guaranteed to exist. Replaces old scripts and executes when a character is added.

now i get this. DeathGui is not a valid member of PlayerGui “Players.DevForumWanu10.PlayerGui”

Can you show a picture of what’s inside StarterGui?

Alright, I guess you should use WaitForChild on the DeathGui. Like this:

local ui = player.PlayerGui:WaitForChild("DeathGui")
local message = ui:WaitForChild("DeathFrame")
local title = message:WaitForChild("Title")

The WaitForChild(name) method will wait until the object your looking for has been loaded.

2 Likes

it works! Thank you for helping

1 Like

quistion tho.
if i die there is still a big gap at the top and you can see the game there.

On the Gui, turn IgnoreGuiInset on. The GuiInset is the space Roblox’s Topbar takes up.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.