Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you make damage numbers show based on the actual value instead of how much health an npc has?

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

0

You would have to use SpawnUI() in the actual script where it damages the NPC.

0

ok but what line do i put that in?

0

I can't tell if you didn't send the script where you damaged the NPC.

0

what do you mean by that?

0

also this is the script i used for damage numbers if thats what you're saying

1 answer

Actually, you can do this with a script in the npc which may be more efficient in case it takes damage from something else. To do this, just subtract the previous health value by the current one. For example, if the npc has 3 health and Is damaged 2, it now has one health. But the npc doesn't know this, so you can just use a little math, so it's previous minus current, 3 - 1, which of course is 2, 2 damage yeah? And you might wonder how to get the previous health value, well just set it after evertime the npc takes damage, then use an event to detect damage or healed (negative number for added health) and there you go. Like this

local previousHealth = npc.Humanoid.Health

npc.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()

     local damageTaken = previousHealth - npc.Humanoid.Health

     if damageTaken < 0 then -- less than 0, so the humanoid was actually healed

          print("healed by " ..(damageTaken * -1) .." points") -- the -1 just switches the symbol because for example, taking 1 damage is the same as healing -1 points, so the symbol must be switched for making the context healing

     else -- it was over 0, which means health was taken

          print("damaged by " ..damageTaken .."points")

     end

     previousHealth = npc.Humanoir.Health -- sets it afterwards for same effect, now when it's damaged again, it will calculate the value based on the health right now as being the previous health.

end)

Hope this helps :3

4 comments

0

the script is in starter player

0

also i don't want numbers to show when it heals

0

and i don't want it to print everytime an npc takes damage

0

Hello, i see that you dont want print or heal, please dont write a comment as i dont need to know this, jjst dont include those parts in your code. Also about the first comment you made that the script is in starterplayer, just make it work for that then. Im not giving you the exact code, im giving you an idea of how to do it, your welcome.

This discussion is preserved as it was. New answers, comments, and votes are closed.