Death Message Script Help Needed

I am currently trying to make random death messages that show when a player dies.
Any help is wanted. Thanks!
Here is the script.
local DeathMessages = {

“Test 0_0”,

“Testing ye”,

“testinggg”,

}

while true do

if humanoid.Health <= 0 then

PlayerGui.ScreenGui.Enabled = true

PlayerGui.ScreenGui.TextLabel.Text = DeathMessages[math.random(1, #DeathMessages)]

end

end

I rearranged your code to work with the Humanoid Died event to make your script not need a loop. I put this code in a Local Script in StarterPlayerScripts.

local DeathMessages = {"Death Message 1","Death Message 2","Death Message 3"}

local LOCAL_PLAYER = game:GetService("Players").LocalPlayer

LOCAL_PLAYER.CharacterAdded:Connect(function(Character)
	Character:WaitForChild("Humanoid").Died:Connect(function()
		LOCAL_PLAYER.PlayerGui.ScreenGui.TextLabel.Text = DeathMessages[math.random(1,#DeathMessages)]
		LOCAL_PLAYER.PlayerGui.ScreenGui.Enabled = true
	end)
end)
2 Likes

I’am going to test something… Wait for it!

1 Like

Adding on to what @xZylter said, you may want to hide the GUI on the Player.CharacterAdded event, so it’s hidden when they spawn back in.

2 Likes

I’ll test it out. Thanks! ----------

Lol and adding on to that, you could set the UI’s ResetOnSpawn property to false, and have a custom wait time after the characterAdded event for it to disappear

2 Likes

Just as i thought, this line:

local Character = LOCAL_PLAYER.CharacterAdded:Wait()

Would “error”, basically, if you died once, the script wouldn’t work, @OP, you will have to connect the playerObject.CharacterAdded event to an function instead.

Yeah I found that out after testing it. I sorta threw this script together quickly. I was considering making this a Server Script but that would go out of the way of this post.

1 Like

How should I fix this? Sorry I am late to scripting :confused:

I thought of that, because i noticed that when you do something like this:

local plr = game.Players.LocalPlayer

local char = plr.Character or plr.CharacterAdded:Wait()

It would only run once, breaking after the player dies, the only way for this method to work, is by making the char variable be updated, for example when an tool is equipped and you LeftClick, and the char variable is inside, it would be updated, every time the user clicks.

Edited my old reply with the new code. Just make sure the GUI is set to reset on respawn.

1 Like

Still not working for me off of death.

Did you set the ScreenGUI’s ResetOnSpawn to true? Also, is it in StarterPlayerScripts?

Yep --------------------------------------

Can you please check if there are any errors in the output, whenever you are currently running something?

There is no errors in the output when I die.

from what i read

when i do tables that needs randomizing

i do this

local Table = {"FerbZides", "Dead"}

for i = 1, #Table do
     print(i)
end

Are the more than 1 ScreenGui’s in the StarterGui, and if there are, those these ScreenGui’s have the same name?(The default name.)

Nope only one. ------------------

There is only one answer to this

Is your text label

visible