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
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)
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.