Death Message Script

Hello! I want to make a script that randomly chooses a message to appear when the character dies, like how it happens in The Wild West. Here is my script.

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local LocalPlayerCharacter = LocalPlayer.Character
local gui = LocalPlayer.PlayerGui.DeathGui
local text = gui.TextLabel
local hum = LocalPlayerCharacter:FindFirstChild("Humanoid")

if hum.Health == 0 then
	gui.Enabled = true
	random = math.random(1,3)
	if random == 1 then
		text.Text = "1"
	elseif random == 2 then
		text.Text = "2"
	elseif random == 3 then
		text.Text = "3"
	end
	wait(3)
	gui.Enabled = false
end

The error in the output is
Players.cweego.PlayerGui.DeathGui.LocalScript:8: attempt to index nil with ‘Health’

I’m not sure what’s wrong with the script.

Thanks,
cweego

1 Like

Sometimes the player’s character doesn’t exist initially when you try to access it (doing LocalPlayer.Character before the character has loaded)
To get around this, you can do

local LocalPlayerCharacter = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

which will either a) get the LocalPlayer’s character normally, or b) wait for the CharacterAdded event to fire on the LocalPlayer (which then returns the character).
You may run into the same problems with LocalPlayer.PlayerGui - trying to access it before it exists. To fix this, try a :WaitForChild("PlayerGui"), which will yield the script until the child with the given name is found:

local gui = LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("DeathGui")

Chaining WaitForChilds is good practice, especially with things you can’t be certain will exist when a script is run immediately.
Another good bit of practice that will help with scalability in the future (if you want to add more death messages), would be to store the messages in a table, then to randomly index your table using math.random() like so:

local messages = {
    "Message one", 
    "Message two",
    "Message three",
--    ..etc
}
local randomIndex = math.random(#messages) -- Generate a random number between 1 and the length of the messages table
local randomMessage = messages[randomIndex] -- Use the randomly generated number to index your messages table (pick a value from the table randomly)
print(randomMessage) -- possible example: "Message two"
-- This can also be shortened into one line:
local randomMessage = messages[math.random(#messages)] -- Combining the first two lines from above

As others have said, your script will only work one time, it will not make the check that the humanoid’s health is 0 more than once.
You should utilize an event here Humanoid.Died which will listen for the humanoid’s health to be 0 and run a connected function when that event is fired.

hum.Died:Connect(function()
    print("Humanoid has died!")
end)
2 Likes

you have to use a repeat wait()

repeat wait() until LocalPlayer.Character
repeat wait() until LocalPlayer.Character.Humanoid
and you should use
hum.Died:Connect(function()

end)

this is the current script i use for death messages:

repeat wait() until game.Players.LocalPlayer.Character.Humanoid
tweenservice = game:GetService("TweenService")
game.Players.LocalPlayer.Character.Humanoid.Died:Connect(function()
	local mathrandom = math.random(1,5)
	script.Parent.InvisFrame.Visible = true
	script.Parent.InvisFrame:TweenPosition(UDim2.new(0.333, 0,0.749, 0),"InOut","Sine",0.5)
	script.Parent.InvisFrame.text.Text = game.Players.LocalPlayer.Name
	if mathrandom == 2 then
		script.Parent.InvisFrame.TextLabel.Text = "How could this be?"
	end
	if mathrandom == 1 then
		script.Parent.InvisFrame.TextLabel.Text = "Impossible!"
	end
	if mathrandom == 3 then
		script.Parent.InvisFrame.TextLabel.Text = "GG"
	end
	if mathrandom == 4 then
		script.Parent.InvisFrame.TextLabel.Text = "Oof!"
	end
	if mathrandom == 5 then
		script.Parent.InvisFrame.TextLabel.Text = "This won't be the last of me."
	end
	wait(3)
	local tweeninfo = TweenInfo.new(0.21, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	local tween = tweenservice:Create(script.Parent.InvisFrame.CoolLookingFrame, tweeninfo, {Transparency = 1})
	tween:Play()
	local tweeninfo = TweenInfo.new(0.21, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	local tween = tweenservice:Create(script.Parent.InvisFrame:WaitForChild("text"), tweeninfo, {Transparency = 1})
	tween:Play()
	local tweeninfo = TweenInfo.new(0.21, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	local tween = tweenservice:Create(script.Parent.InvisFrame:WaitForChild("TextLabel"), tweeninfo, {Transparency = 1})
	tween:Play()
	wait(0.24)
	script.Parent.Enabled = false

end)
2 Likes

The way scripts work is that its only going to check if the Player’s Health is equal to 0 one time.

Heres the solution:

hum.Died:Connect(function()
     -- your code goes here
end)

Also, when trying to access the Player’s Character sometimes it hasn’t loaded so change your variable to this:

local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

If the character isn’t ready, the script will pause for a second and wait for it.

You can modify this to make it have random messages by changing line 36 in the localscript to this: TextLabel.Text = DeathMessages[math.random(1, #DeathMessages)]

and then adding local DeathMessages = {"Dead", "Pwned", "Oofed"} near the top of the localscript