Damage Text Random Number?

I am trying to have the script display the number of damage the NPC has taken, it works, but it displays a series of 9’s before the actual number. I am quite confused.

Visual

local humanoid = script.Parent
local cHealth = humanoid.Health
 
humanoid.HealthChanged:Connect(function(health)
    local Gui = script.DamageCounter:Clone()
    local change = math.floor(cHealth - health)
    if change == 0 then return end
    Gui.Parent = humanoid.Parent.Head
	Gui.TextLabel.Visible = true
    Gui.TextLabel.Text = "Damage: "..change
    wait(1)
	Gui.TextLabel.Visible = false
end)

Could you please show us a visual display?

health argument is automatically the change already, if I reckon correctly. In other words, health is the new value. I am quite unsure what cause the numbers to bump over thousands.

Here’s a suggestion, analyse the values with the print to track the values in console:
print(cHealth, health, change)

I just edited this post with one.

Here is what was printed out:
cHealth: 999999
Health: 11 (correct)
Change: 999988
After killing it printed
cHealth: 14
Health: 10 (correct)
Change: 4

BUT it still displayed the numbers incorrectly.

Check the humanoid’s health to see if something is out of place. I’m pretty sure this is outside the script.

Alright, so,

Humanoid.Health is changing, it is currently at the top of the script which means it will not constantly update unless told to do so, try putting the cHealth variable inside the HealthChanged listener. My best guess is that the Humanoid’s cHealth is automatically set back to a different number from 999999 when the game is played.

Tell me if this helped at all

When inside of the listener, it does not work.

Perhaps try putting this all in a seperate place? It may be another script in your current place causing this error.

I did printing debug and I found out it stops at

    if change == 0 then return end

EDIT: I removed it but now it only displays 0.

humanoid.HealthChanged:Connect(function(health)
    local change = math.floor(cHealth - health)
    if change == 0 then return end
    local Gui = script.DamageCounter:Clone()
    Gui.Parent = humanoid.Parent.Head
	Gui.TextLabel.Visible = true
    Gui.TextLabel.Text = "Damage: "..change
    wait(1)
	Gui.TextLabel.Visible = false
end)

Try this

It keeps adding up depending on how much it hits. If it hits for 2 then for 3 it combines at 5. And once dead, it stops working.

Is this damage gui supposed to be visible by everyone? If not then you can do this in the weapon’s script and get the hit players health and minus it by the damage you gave them.

I did not know that, I will give it a try, thank you!

Glad I could be of assistance! If you need further help just feel free to message me or reply back in this thread and I will be more than happy to help.