DamageIndicators - Free, Customizable, and Easy to Implement
I’m not a UI designer, so you can customize the look of your indicators to fit your exact needs! The size of the text also is relative to the amount of damage taken!
Implementation
DamageIndicators is a class with 2 primary methods of implementation. The first is direct, while the other will bind to ALL NPCs in your game. In order for the class to work, the NPC must have a head and humanoid.
Direct
DamageIndicator.new(workspace.CheekySquid)
This will bind to the NPC you have indicated. When the NPC dies or is removed, it automatically unbinds itself.
Remade it so it works for players and npcs only did npcs 2 since i got nun in my game
-- Damage Indicator
-- CheekySquid // Made live on Twitch! // CheekyVisuals on Twitch
-- 2/7/2020
--[[
Methods:
public metatable new(instance)
public void Bind(instance)
public void Destroy()
public void SpawnUI(number amount)
public Color3 GetIndicatorColor(number amount)
public static void BindToAllCharacters()
Attributes:
public number currentHealth
public instance humanoid
public instance head
public instance
private metatable _maid
Usage Examples:
DamageIndication.new(workspace.CheekySquid)
DamageIndication.BindToAllCharacters()
--]]
--// Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
--// 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:FindFirstChildWhichIsA("Humanoid")
self._head = instance:FindFirstChild("Head")
self._instance = instance
self._maid = Maid.new()
self:Bind()
return self
end
function DamageIndication:Bind()
assert(self._humanoid, "Instance does not contain a humanoid")
assert(self._head, "Instance does not contain a head part")
self.currentHealth = self._humanoid.Health
self._maid:GiveTask(self._humanoid.HealthChanged:Connect(function(newHealth)
if newHealth < self.currentHealth then
local damage = self.currentHealth - newHealth
self.currentHealth = newHealth
self:SpawnUI(damage)
else
self.currentHealth = newHealth
end
end))
self._maid:GiveTask(self._instance.AncestryChanged:Connect(function()
if not self._instance:IsDescendantOf(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
label.Text = tostring(math.floor(math.abs(amount)))
label.TextColor3 = self:GetIndicatorColor(amount)
-- Position the indicator above the head
local attachment = Instance.new("Attachment")
attachment.Position = Vector3.new(0, 3, 0) -- Adjust this offset as necessary
attachment.Parent = self._head
indicator.Adornee = attachment
indicator.Parent = game.Workspace
-- Tween the indicator upwards and fade out
local tweenPosition = TweenService:Create(indicator, TweenInfo.new(1, Enum.EasingStyle.Linear), {
StudsOffset = Vector3.new(0, 2, 0) -- Adjust this offset as necessary
})
local tweenTransparency = TweenService:Create(label, TweenInfo.new(1, Enum.EasingStyle.Linear), {
TextTransparency = 1,
TextStrokeTransparency = 1
})
tweenPosition:Play()
tweenTransparency:Play()
tweenTransparency.Completed:Connect(function()
indicator:Destroy()
attachment:Destroy()
end)
end
function DamageIndication:GetIndicatorColor(amount)
return Color3.fromRGB(255, 0, 0)
end
function DamageIndication:Destroy()
self._maid:Destroy()
end
function DamageIndication.BindToAllCharacters()
for _, instance in ipairs(workspace:GetDescendants()) do
if instance:IsA("Model") and instance:FindFirstChildWhichIsA("Humanoid") and instance:FindFirstChild("Head") then
DamageIndication.new(instance)
end
end
workspace.ChildAdded:Connect(function(child)
if child:IsA("Model") and child:FindFirstChildWhichIsA("Humanoid") and child:FindFirstChild("Head") then
DamageIndication.new(child)
end
end)
for _, player in ipairs(Players:GetPlayers()) do
if player.Character then
DamageIndication.new(player.Character)
end
player.CharacterAdded:Connect(function(character)
DamageIndication.new(character)
end)
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
DamageIndication.new(character)
end)
end)
end
return DamageIndication