Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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?

0
what is 'v' WeBuiltOurOwnWorld 6 — 5y
0
o sorry v is the humanoid Ido_nothaveUsername 213 — 5y
0
I would changed that function to the Humanoid.HealthChanged function, and see where you go from there. BuDeep 214 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

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)
Ad

Answer this question