Damage indicator doesn't indicate the gui

I’m trying to learn how to make a damage indicator but the problem is the the script that I have right now is not working. the script doesn’t indicate the gui

local TS = game:GetService("TweenService")
local Tinfo = TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)

function roundNumber(num, numDecimalPlaces)
	return tonumber(string.format(" " .. (numDecimalPlaces or 0) .. "f", num))
end

local indcator = game.ReplicatedStorage.Indicator

local Red = Color3.fromRGB(255, 0, 0)
local Green = Color3.fromRGB(95,111,64)

for i, v in pairs(game.Workspace:GetChildren()) do
	if v:IsA("Model") then
		if v:FindFirstChildWhichIsA("Humanoid") then
			local hum = v:FindFirstChildWhichIsA("Humanoid")
			local humH = hum.Health

			hum.HealthChanged:Connect(function(Health)
				if Health < humH and Health > 0 then

					local Damage = roundNumber(humH-Health,1)
					print(Damage.." Damage was taken")

					local BoardClone = indcator:Clone()
					BoardClone:FindFirstChild("Damage").Damage = Damage

					local maxH = hum.MaxHealth
					BoardClone.Damage.TextColor3 = Green:lerp(Red, Damage / maxH)


					BoardClone.Parent = v.HumanoidRootPart

					BoardClone.Damage:TweenSize(UDim2.new(1,0,1,0), "InOut", "Quint", 0.3)

					wait(0.3)

					local UIupTween = TS:Create(BoardClone, Tinfo, {StudsOffset = BoardClone.StudsOffset + Vector3.new(0,1,0)})
					local textFade = TS:Create(BoardClone.Damage, Tinfo, {TextTransparency = 1})

					UIupTween:Play()
					textFade:Play()

					game:GetService("Debris"):AddItem(BoardClone, 0.5)
				end
				humH = hum.Health
			end)
		end
	end
end
1 Like

Screenshot 2022-03-24 205230

1 Like

If this is the line that is trying to find the gui, the name is misspelled and instead should be DamageIndicator. Also, you are supposedly referencing “Damage” in the gui, which is actually named Num.

1 Like

this the script after changing it.it still not work. after I decrease my health the gui didn’t show up

local TS = game:GetService("TweenService")
local Tinfo = TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)

function roundNumber(num, numDecimalPlaces)
	return tonumber(string.format(" " .. (numDecimalPlaces or 0) .. "f", num))
end

local indcator = game.ReplicatedStorage.Indicator

local Red = Color3.fromRGB(255, 0, 0)
local Green = Color3.fromRGB(95,111,64)

for i, v in pairs(game.Workspace:GetChildren()) do
	if v:IsA("Model") then
		if v:FindFirstChildWhichIsA("Humanoid") then
			local hum = v:FindFirstChildWhichIsA("Humanoid")
			local humH = hum.Health

			hum.HealthChanged:Connect(function(Health)
				if Health < humH and Health > 0 then

					local Damage = roundNumber(humH-Health,1)
					print(Damage.." Damage was taken")

					local DamageIndicator = indcator:Clone()
					DamageIndicator:FindFirstChild("Num").Damage = Damage

					local maxH = hum.MaxHealth
					DamageIndicator.Damage.TextColor3 = Green:lerp(Red, Damage / maxH)


					DamageIndicator.Parent = v.HumanoidRootPart

					DamageIndicator.Damage:TweenSize(UDim2.new(1,0,1,0), "InOut", "Quint", 0.3)

					wait(0.3)

					local UIupTween = TS:Create(DamageIndicator, Tinfo, {StudsOffset = DamageIndicator.StudsOffset + Vector3.new(0,1,0)})
					local textFade = TS:Create(DamageIndicator.Damage, Tinfo, {TextTransparency = 1})

					UIupTween:Play()
					textFade:Play()

					game:GetService("Debris"):AddItem(DamageIndicator, 0.5)
				end
				humH = hum.Health
			end)
		end
	end
end

You did fix the line of code that i mentioned, but the rest are still use DamageIndicator.Damage. I also suggest you use a variable for the text label, instead of constantly referencing it. Also, is there any other error in the console?

1 Like

there is no error but it just cant fusion right

Where is the script located? It appears it checks for humanoids in workspace, but if it can’t find one that might be the reason it doesn’t print anything. You should instead do either: Make it a local script that fires to a server script when you take damage, or utilize workspace.ChildAdded or game.Players.PlayerAdded (for players only).

And I noticed that the variable DamageIndicator is the part, not the Gui.
Consider changing

local DamageIndicator = indcator:Clone()

to

local indicatorPart = indcator:Clone()
local DamageIndicator = indicatorPart:FindFirstChild("DamageIndicator")
1 Like

the script location is in the sever script service

I fix the line up that you have suggest and change all that use the misspelled words. but I still confuse about why cant the gui show up when take damage or health decrease

for i, v in pairs(game.Workspace:GetChildren()) do
	if v:IsA("Model") then
		if v:FindFirstChildWhichIsA("Humanoid") then
			local hum = v:FindFirstChildWhichIsA("Humanoid")
			local humH = hum.Health

			hum.HealthChanged:Connect(function(Health)
				if Health < hum and Health > 0 then

					local Damage = roundNumber(humH-Health,1)
					print(Damage.." Damage was taken")

					local indicatorPart = indcator:Clone()
					local DamageIndicator = indicatorPart:FindFirstChild("DamageIndicator")
					local Num = indicatorPart.DamageIndicator:WaitForChild("Num")
					Num.Text = Damage

					local maxH = hum.MaxHealth
					DamageIndicator.Damage.TextColor3 = Green:lerp(Red, Damage / maxH)


					DamageIndicator.Parent = v.HumanoidRootPart

					DamageIndicator.Damage:TweenSize(UDim2.new(1,0,1,0), "InOut", "Quint", 0.3)

					wait(0.3)

					local UIupTween = TS:Create(DamageIndicator, Tinfo, {StudsOffset = DamageIndicator.StudsOffset + Vector3.new(0,1,0)})
					local textFade = TS:Create(DamageIndicator.Damage, Tinfo, {TextTransparency = 1})

					UIupTween:Play()
					textFade:Play()

					game:GetService("Debris"):AddItem(DamageIndicator, 0.5)
				end
				humH = hum.Health
1 Like

As I mentioned, this could be because game.Workspace:GetChildren() gets workspace's children before the players load in, so instead you should connect it to an event workspace.ChildAdded like so:

workspace.ChildAdded:Connect(function(v)
	if v:IsA("Model") then
		if v:FindFirstChildWhichIsA("Humanoid") then
			local hum = v:FindFirstChildWhichIsA("Humanoid")
			local humH = hum.Health

			hum.HealthChanged:Connect(function(Health)
				if Health < humH and Health > 0 then

					local Damage = roundNumber(humH-Health,1)
					print(Damage.." Damage was taken")

					local indicatorPart = indcator:Clone()
					local DamageIndicator = indicatorPart:FindFirstChild("DamageIndicator")
					local Num = DamageIndicator:WaitForChild("Num")
					Num.Text = Damage

					local maxH = hum.MaxHealth
					Num.TextColor3 = Green:lerp(Red, Damage / maxH)


					DamageIndicator.Parent = v.HumanoidRootPart

					Num:TweenSize(UDim2.new(1,0,1,0), "InOut", "Quint", 0.3)

					wait(0.3)

					local UIupTween = TS:Create(DamageIndicator, Tinfo, {StudsOffset = DamageIndicator.StudsOffset + Vector3.new(0,1,0)})
					local textFade = TS:Create(DamageIndicator.Damage, Tinfo, {TextTransparency = 1})

					UIupTween:Play()
					textFade:Play()

					game:GetService("Debris"):AddItem(DamageIndicator, 0.5)
				end
				humH = hum.Health
			end)
		end
	end
end)

ok i have change the script and use the script that you have given me it still doesn’t work . i change it to player only to see does it work or not.

game.Players.PlayerAdded:Connect(function(v)
	if v:IsA("Player") then
		if v:FindFirstChildWhichIsA("Humanoid") then
			local hum = v:FindFirstChildWhichIsA("Humanoid")
			local humH = hum.Health
print(" works ")
			hum.HealthChanged:Connect(function(Health)
				if Health < humH and Health > 0 then

					local Damage = roundNumber(humH-Health,1)
					print(Damage.." Damage was taken")

					local indicatorPart = indcator:Clone()
					local DamageIndicator = indcator:FindFirstChild("DamageIndicator")
					local Num = DamageIndicator:WaitForChild("Num")

					local maxH = hum.MaxHealth
					Num.TextColor3 = Green:lerp(Red, Damage / maxH)


					DamageIndicator.Parent = v.HumanoidRootPart

					Num:TweenSize(UDim2.new(1,0,1,0), "InOut", "Quint", 0.3)

					wait(0.3)

					local UIupTween = TS:Create(DamageIndicator, Tinfo, {StudsOffset = DamageIndicator.StudsOffset + Vector3.new(0,1,0)})
					local textFade = TS:Create(DamageIndicator.Damage, Tinfo, {TextTransparency = 1})

					UIupTween:Play()
					textFade:Play()

					game:GetService("Debris"):AddItem(DamageIndicator, 0.5)
				end
				humH = hum.Health
			end)
		end
	end
end)
1 Like

In this script you are checking if the player (v) has the humanoid, however the actual character is v.Character. Also, it should actually be v.CharacterAdded, since the character can respawn more than once.

game.Players.PlayerAdded:Connect(function(v)
	if v:IsA("Player") then
		v.CharacterAdded:Connect(function(v)
			if v:FindFirstChildWhichIsA("Humanoid") then
				local hum = v:FindFirstChildWhichIsA("Humanoid")
				local humH = hum.Health
print(" works ")
				hum.HealthChanged:Connect(function(Health)
					if Health < humH and Health > 0 then
	
						local Damage = roundNumber(humH-Health,1)
						print(Damage.." Damage was taken")
	
						local indicatorPart = indcator:Clone()
						local DamageIndicator = indcator:FindFirstChild("DamageIndicator")
						local Num = DamageIndicator:WaitForChild("Num")
	
						local maxH = hum.MaxHealth
						Num.TextColor3 = Green:lerp(Red, Damage / maxH)
	
	
						DamageIndicator.Parent = v.HumanoidRootPart
	
						Num:TweenSize(UDim2.new(1,0,1,0), "InOut", "Quint", 0.3)
	
						wait(0.3)
	
						local UIupTween = TS:Create(DamageIndicator, Tinfo, {StudsOffset = DamageIndicator.StudsOffset + Vector3.new(0,1,0)})
						local textFade = TS:Create(DamageIndicator.Damage, Tinfo, {TextTransparency = 1})
	
						UIupTween:Play()
						textFade:Play()
	
						game:GetService("Debris"):AddItem(DamageIndicator, 0.5)
					end
					humH = hum.Health
				end)
			end
		end)
	end
end)

i have done what you have suggest and it still not work . I’m so confuse about why cant it show the gui

1 Like

i have tried this but doesn’t work

1 Like

Why is it shown as working at the end of the video then?

I have test the script and it doesn’t work

1 Like