I'm trying to create a script that makes damage numbers pop up(billboard GUIs) when a humanoid has taken damage, what I am doing currently is this: note that I have defined all variables *v is the humanoid of the characters
v.Changed:connect(function (p) if (p == "Health") then print(v.health) local delta = h - v.Health if delta >= 1 then local gui = script:WaitForChild("HurtGui"):clone() gui.Parent = v.Parent:FindFirstChild("Torso") gui.Adornee = gui.Parent gui.StudsOffsetWorldSpace = Vector3.new(math.random(xoffset*2)-xoffset,math.random(yoffset*2)-yoffset,math.random(zoffset*2)-zoffset) gui:WaitForChild("TextLabel").Text = "" .. delta wait(1) gui:Destroy() end h = v.Health end end)
I think it's pretty inefficient and I am still getting bugs like large numbers popping up when the humanoid didn't take that much damage.
How would I fix this?
What I do is do this operation 100-(actual health) so if you had 100 health and you decrease your health to 10 the operation will give 90 then, set a global with the actual health to do the same operation but 100 is changed with 10 so if your health decreases to 2 then you do 10-2 and you get 8 Here is the source
local ActualHealth = 100 v.Changed:connect(function (p) if (p == "Health") then print(v.health) local delta = ActualHealth - v.Health ActualHealth = v.Health if delta >= 1 then local gui = script:WaitForChild("HurtGui"):clone() gui.Parent = v.Parent:FindFirstChild("Torso") gui.Adornee = gui.Parent gui.StudsOffsetWorldSpace = Vector3.new(math.random(xoffset*2)-xoffset,math.random(yoffset*2)-yoffset,math.random(zoffset*2)-zoffset) gui:WaitForChild("TextLabel").Text = "" .. delta wait(1) gui:Destroy() end end end)