Issuse with animated text gui not appearing when npc is killed

Hello, I have made an animated text gui. There is one slight error though, I need the text to appear when the npc has no health left.

The script is shown below:

local Text

function TextSet(word)
	Text = word
	for i = 1, #Text do
		TextLabel.Text = string.sub(Text, 1, i)
		SoundEffect()
		TextLabel.TextColor3 = Color3.fromRGB(255,255,255)
		wait(0.05)
	end
end

TextSet("Hello")
wait(5)
if game.Workspace.npc.Humanoid.Health == 0 then
       TextSet("You have beaten the npc!")
 end

But when I change the npcs health to 100 it works.
Any solutions for this? Help will be appreciated!

1 Like

You might wanna implement .HealthChanged event for npc’s humanoid instead

EDIT: my bad yeah, .Died event exist

You can use the Humanoid.Died event for this! You also might want to find another way to define your NPC as if there are multiple NPCs in workspace, it will look for a random NPC, not the intended one. I will leave a variable for you to define in my example.

Example:

local Text
local NPC = game.Workspace.npc --The NPC

function TextSet(word)
   Text = word

   for i = 1, #Text do
   	TextLabel.Text = string.sub(Text, 1, i)
   	SoundEffect()
   	TextLabel.TextColor3 = Color3.fromRGB(255,255,255)
   	wait(0.05)
   end
end

TextSet("Hello")
wait(5)
NPC.Humanoid.Died:Connect(function() --Check when the NPC dies.
   TextSet("You have beaten the NPC!")
end)
2 Likes

Thanks this solution worked :slight_smile:.