HOW TO MAKE A CHARACTER DAMAGE INDICATOR
Let´s begin!
First we are going to create a Server Script inside ServerScriptService.
Then open the Script and start Scripting!
First we are going to declare the services we will be using.
---------- SERVICES ----------
local Players = game:GetService("Players")
local Debris = game:GetService("Debris") -- the service that will help delete objects after a certain time
local TS = game:GetService("TweenService") -- for smooth animation
- Players: Manages all players in the game.
- Debris: Helps remove objects automatically after a set time.
- TweenService(TS): Used to create smooth animations (like fading effects).
Next, we define a function called createHighlight()
that creates a red glow effect on a character when they take damage.
---------- FUNCTIONS ----------
local function createHighlight(character:Model) -- this function will create and return the highlight for us
local highlight = Instance.new("Highlight")
highlight.OutlineTransparency = 1 -- hides the outline stroke
highlight.DepthMode = Enum.HighlightDepthMode.Occluded -- makes sure that the effect does not overlay over other objects
highlight.Parent = character -- puts in the character
Debris:AddItem(highlight,0.5) -- deletes the effect after 0.5 seconds
TS:Create(highlight,TweenInfo.new(0.5),{FillTransparency = 1}):Play()
end
- We create a Highlight object that makes the whole character glow.
- The outline is removed (
OutlineTransparency = 1
) so only the body gets affected. - The effect is behind other objects (
DepthMode = Occluded
), so it doesn’t look weird if the player is behind something. - The highlight is added to the character model.
- We remove the effect after 0.5 seconds using
Debris:AddItem()
. - A tween animation fades out the effect smoothly.
Now we detect when a player joins and when their character spawns.
Players.PlayerAdded:Connect(function(player) -- fires when a player joins the server
-- player just joined the game
player.CharacterAdded:Connect(function(character) -- fires when the character of the player is added or when the player respawns
-- player has just spawned
end)
end)
- PlayerAdded fires when a player joins.
- CharacterAdded fires when their character spawns or respawns.
We store the player’s current health in an attribute so we can check if it changes later.
player.CharacterAdded:Connect(function(character) -- fires when the character of the player is added or when the player respawns
local hum = character:WaitForChild("Humanoid") --wait for the players character humanoid
character:SetAttribute("LastHealthAmount",hum.Health) -- stores the player´s current health
end)
We set up an event to detect when the player’s health changes.
player.CharacterAdded:Connect(function(character) -- fires when the character of the player is added or when the player respawns
...
hum:GetPropertyChangedSignal("Health"):Connect(function() -- fires when the health of the player changes
-- player took damage or their health changed
end)
end)
This runs whenever the player’s health changes (when they take damage or heal).
Now we check if the player lost health (not healing).
hum:GetPropertyChangedSignal("Health"):Connect(function() -- fires when the health of the player changes
local changedhealth = character:GetAttribute("LastHealthAmount") - hum.Health -- substracting the current health from the lasthealth stored will give us how many health was lost
end)
This calculates how much health was lost by subtracting the new health from the old health.
We only run the effect if the player actually took damage (not healing or respawning). And then finally, we update the stored health so we always compare correctly next time.
hum:GetPropertyChangedSignal("Health"):Connect(function() -- fires when the health of the player changes
local changedhealth = character:GetAttribute("LastHealthAmount") - hum.Health -- substracting the current health from the lasthealth stored will give us how many health was lost
if changedhealth > 0 and hum.Health > 0 then --making sure the damaged health is greater than 0, to avoid this effect from happening when they are healing
createHighlight(character)
end
character:SetAttribute("LastHealthAmount",hum.Health) -- update the lasthealth to the new health amount
end)
At the end our final Code for the Damage Indicator will be the following:
---------- SERVICES ----------
local Players = game:GetService("Players")
local Debris = game:GetService("Debris") -- the service that will help delete objects after a certain time
local TS = game:GetService("TweenService") -- for smooth animation
---------- FUNCTIONS ----------
local function createHighlight(character:Model) -- this function will create and return the highlight for us
local highlight = Instance.new("Highlight")
highlight.OutlineTransparency = 1 -- hides the outline stroke
highlight.DepthMode = Enum.HighlightDepthMode.Occluded -- makes sure that the effect does not overlay over other objects
highlight.Parent = character -- puts in the character
Debris:AddItem(highlight,0.5) -- deletes the effect after 0.5 seconds
TS:Create(highlight,TweenInfo.new(0.5),{FillTransparency = 1}):Play()
end
Players.PlayerAdded:Connect(function(player) -- fires when a player joins the server
player.CharacterAdded:Connect(function(character) -- fires when the character of the player is added or when the player respawns
local hum = character:WaitForChild("Humanoid") --wait for the players character humanoid
character:SetAttribute("LastHealthAmount",hum.Health) -- stores the player´s current health
hum:GetPropertyChangedSignal("Health"):Connect(function() -- fires when the health of the player changes
local changedhealth = character:GetAttribute("LastHealthAmount") - hum.Health -- substracting the current health from the lasthealth stored will give us how many health was lost
if changedhealth > 0 and hum.Health > 0 then --making sure the damaged health is greater than 0, to avoid this effect from happening when they are healing
createHighlight(character)
end
character:SetAttribute("LastHealthAmount",hum.Health) -- update the lasthealth to the new health amount
end)
end)
end)
Feel free to copy and paste with no problem
The End
Well that will be, if there are any corrections or anything you will like to point out, do so down below. Remember if you do not want to follow the tutorial above, you can just get the file (.rbxm) on my Patreon , you can also find other scripting stuffs I have done there aswell. Thanks