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

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