Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to detect when a player takes damage and how to find how much?

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?

3 comments

0

what is 'v'

0

o sorry v is the humanoid

0

I would changed that function to the Humanoid.HealthChanged function, and see where you go from there.

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.