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