Damage Indicator number not adding up correctly

I am not completely sure why it wont add up like expected. It is just one line of code.

-- Perfectly working code

-- damage numbers
local DamageNumber = game:GetService('ReplicatedStorage').DamageNumber:Clone()
			
DamageNumber.Parent = humanoid.Parent.Head
DamageNumber.TextLabel.Text = tostring(damage)

--=======================
-- Broken
-- damage numbers
local DamageNumber = game:GetService('ReplicatedStorage').DamageNumber:Clone()
			
DamageNumber.Parent = humanoid.Parent.Head
DamageNumber.TextLabel.Text = tostring(damage)
			
if humanoid.Parent.Head:FindFirstChild('DamageNumber') then
	DamageNumber.TextLabel.Text = tostring(tonumber(DamageNumber.TextLabel.Text + damage)) -- Breaks
end

Clips:

1.wmv (161.6 KB)
2.wmv (239.8 KB)

Issue:

As you can tell it multiplies the damage number after a single shot. A headshot does 100 and any other limb does 32 but instead of adding up after 2 it just doubles the real number. I believe it is the if statement but not too sure why…

2 Likes

I cannot get your video to play at all but I assume you mean its going from 200 to 200200?

1 Like

Why are you cloning the DamageNumber in ReplicatedStorage?
If you take off :Clone() don’t you get the same value?

Forgive my ignorance, but is
DamageNumber.TextLabel.Text = tostring(damage)
selecting the variable damage? If so how is damage set because you are using it in your calculation in the broken section?

Try printing all your variables in the bottom section to see which variable breaks the formula:

if humanoid.Parent.Head:FindFirstChild('DamageNumber') then
    print( "Text: ", DamageNumber.TextlLabel.Text , "  damage:   ", damage)
	DamageNumber.TextLabel.Text = tostring(tonumber(DamageNumber.TextLabel.Text + damage)) -- Breaks
end

100 > 200 is what is happening so instead of 100 it goes to 200. 30 > 64 so on and so forth

damage is the damage sent to the server. the gui for the indicator is cloned from the replicated storage to the hit target when the remote event is fired to the player who shot the target. The event fire to the player is giving the client the amount of damage the user did aswell as the targeted player for the parent of the cloned gui

local DamageNumber = game:GetService('ReplicatedStorage').DamageNumber:Clone()
			
DamageNumber.Parent = humanoid.Parent.Head
DamageNumber.TextLabel.Text = tostring(damage)

--FindFirstChild returns the already existing DamageNumber, but then it doesn't get saved anywhere
if humanoid.Parent.Head:FindFirstChild('DamageNumber') then
	--This just adds the new damage to itself and sets the label text
	DamageNumber.TextLabel.Text = tostring(tonumber(DamageNumber.TextLabel.Text + damage))
end

heres what I think you meant to do:

-- = nil is optional but the linter will complain otherwise
local DamageNumber = nil 

--Check if there's already a damage number
DamageNumber = humanoid.Parent.Head:FindFirstChild('DamageNumber')

if DamageNumber == nil then
	--There isn't one, make one now
	DamageNumber = game:GetService('ReplicatedStorage').DamageNumber:Clone()
else
	--There is one, add to it
	damage += tonumber(DamageNumber.TextLabel.Text)
end

--This line is Idempotent if DamageNumber already existed
DamageNumber.Parent = humanoid.Parent.Head
DamageNumber.TextLabel.Text = tostring(damage)
2 Likes

ill give that a try. Ill lyk what happens.

That worked, Thank you for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.