How to make a Character Damage Indicator


HOW TO MAKE A CHARACTER DAMAGE INDICATOR

Youtube Video | Full Game File




In this tutorial, I'll show you how to create a character damage indicator in Roblox, just like in Minecraft! When a player takes damage, their character will briefly flash red, giving a clear vision that they've been hit. I'll walk you through everything step by step from detecting damage to applying the red flash effect.


Let´s begin!


Luckily this tutorial will only involve scripting!

First we are going to create a Server Script inside ServerScriptService.

image

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)

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


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:heart: , you can also find other scripting stuffs I have done there aswell. Thanks :smiley:

4 Likes

Nice tutorial, a few things I’m curious of

why not use Humanoid.HealthChanged?


why not use Highlight.Enabled and flip it rather than creating and destroying it?

3 Likes

for the HealthChanged, I dont actually have a reason rather I prefer using the propertychangedsignal much more.
As for the Highlight.Enabled part, I have had issues in the past where disabling and enabling it does not always work, sometimes it is disabled but the highlight effect is still shown which is a bit annoying. So I prefer to just delete it when I dont need it.