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

How do you make damage numbers show based on the actual value instead of how much health an npc has?

Asked by 3 years ago
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local HttpService = game:GetService("HttpService")

--// Modules
local Maid = require(script.Maid)

--// Assets
local uiTemplate = script.DamageTemplate

--// Objects
local random = Random.new()

local DamageIndication = {}
DamageIndication.__index = DamageIndication

function DamageIndication.new(instance)
    local self = setmetatable({}, DamageIndication)

    self._humanoid = instance:FindFirstChild("Humanoid")
    self._head = instance:FindFirstChild("Head")
    self._instance = instance

    self._maid = Maid.new()

    self:Bind()

    return self
end

--// Bind damage indicator UI to instance, called automaticlaly in constructor
function DamageIndication:Bind()
    assert(self._humanoid, "Instance does not contain a humanoid")
    assert(self._head, "Instance does not contain a humanoid root part")

    self.currentHealth = self._humanoid.Health

    self._maid:GiveTask(self._humanoid.HealthChanged:Connect(function(newHealth)
        local damage = self.currentHealth - newHealth
        self.currentHealth = newHealth

        self:SpawnUI(damage)
    end))

    self._maid:GiveTask(self._instance.AncestryChanged:Connect(function()
        if self._instance.Parent ~= game then
            self:Destroy()
        end
    end))

    self._maid:GiveTask(self._humanoid.Died:Connect(function()
        self:Destroy()
    end))
end

function DamageIndication:SpawnUI(amount)
    local indicator = uiTemplate:Clone()
    local label = indicator.Label
    local maxHealth = self._humanoid.maxHealth

    label.Text = math.floor(math.abs(amount))
    label.TextColor3 = self:GetIndicatorColor(amount, maxHealth)

    local xy = math.clamp(math.abs(amount/maxHealth), 0.6, 1.3)
    label.Size = UDim2.fromScale(xy, xy) 

    --// Randomly disperse to prevent clutter
    local x = random:NextNumber(.2, .8)
    label.Position = UDim2.fromScale(x, 0.5)

    local indicatorTween = TweenService:Create(label, TweenInfo.new(2, Enum.EasingStyle.Quint), {
        Position = UDim2.fromScale(x, -0.5),
        TextTransparency = 1
    })

    indicator.Parent = self._head
    indicatorTween:Play()

    indicatorTween.Completed:Wait()
    indicator:Destroy()
end

function DamageIndication:GetIndicatorColor(amount)
    return amount >= 0 and Color3.fromRGB(255, 255, 255)
end

function DamageIndication:Destroy()
    self._maid:Destroy()
end

--// BONUS: Automatic method that binds to all existing NPC's in your game
function DamageIndication.BindToAllNPCs()
    for _, instance in ipairs(game.Workspace:GetDescendants()) do
        if not instance:IsA("Model") then continue end
        if not instance:FindFirstChild("Humanoid") then continue end
        if not instance:FindFirstChild("Head") then continue end

        DamageIndication.new(instance)
    end
end

return DamageIndication

I want to make it so when i kill an NPC with a weapon it shows the damage value instead of a value based off the npcs health idk which line to change so i pasted the whole code but can anyone help me solve this?

This is what it looks like also btw the gun i'm using does 264k damage not 200k it's showing numbers based on how much health the dummy has unless they have a health value greater than 264k https://gyazo.com/f4bfe6a33de59910ec97bb54f72db9c4

0
You would have to use SpawnUI() in the actual script where it damages the NPC. Neatwyy 123 — 3y
0
ok but what line do i put that in? dylancrazy88 20 — 3y
0
I can't tell if you didn't send the script where you damaged the NPC. Neatwyy 123 — 3y
0
what do you mean by that? dylancrazy88 20 — 3y
0
also this is the script i used for damage numbers if thats what you're saying dylancrazy88 20 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Actually, you can do this with a script in the npc which may be more efficient in case it takes damage from something else. To do this, just subtract the previous health value by the current one. For example, if the npc has 3 health and Is damaged 2, it now has one health. But the npc doesn't know this, so you can just use a little math, so it's previous minus current, 3 - 1, which of course is 2, 2 damage yeah? And you might wonder how to get the previous health value, well just set it after evertime the npc takes damage, then use an event to detect damage or healed (negative number for added health) and there you go. Like this

local previousHealth = npc.Humanoid.Health

npc.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()

     local damageTaken = previousHealth - npc.Humanoid.Health

     if damageTaken < 0 then -- less than 0, so the humanoid was actually healed

          print("healed by " ..(damageTaken * -1) .." points") -- the -1 just switches the symbol because for example, taking 1 damage is the same as healing -1 points, so the symbol must be switched for making the context healing

     else -- it was over 0, which means health was taken

          print("damaged by " ..damageTaken .."points")

     end

     previousHealth = npc.Humanoir.Health -- sets it afterwards for same effect, now when it's damaged again, it will calculate the value based on the health right now as being the previous health.

end)

Hope this helps :3

0
the script is in starter player dylancrazy88 20 — 3y
0
also i don't want numbers to show when it heals dylancrazy88 20 — 3y
0
and i don't want it to print everytime an npc takes damage dylancrazy88 20 — 3y
0
Hello, i see that you dont want print or heal, please dont write a comment as i dont need to know this, jjst dont include those parts in your code. Also about the first comment you made that the script is in starterplayer, just make it work for that then. Im not giving you the exact code, im giving you an idea of how to do it, your welcome. AlexanderYar 788 — 3y
Ad

Answer this question