Make text update when player takes dmg divisible by 5

Hello, everyone, I would like to make it so that whenever a player takes damage (and if it’s divisible by 5 without decimals) then the text string updates

E.G
player takes 5 dmg, text updated to 1
player takes 10 dmg, text updates to 2
if the text is 1 and they take 1 dmg, the text stays as 1 until they take another 4 dmg, which then it’ll update to 2.

1 Like

You’d need to keep the damage taken somewhere like a local variable and add to it when damaged. Then, the string would look something like this

TextLabel.Text = math.floor(DamageTaken / 5)

DamageTaken / 5 will give us something close to the number, so 5 / 5 is 1, 7 / 5 is 1.4. Then using math.floor(), we round it down, so math.floor(7 / 5) is 1.

DamageTaken % 5 = 0

A bit off topic, but I’d like to make it so the text doesn’t go back up if you regenerate, this is my code:

local Character = script.Parent.Parent.Parent.Parent.Parent
local Humanoid = Character.Humanoid
local Player = game.Players:GetPlayerFromCharacter(Character)
local OldHP = Humanoid.Health
local DamageTaken = 0

Humanoid.HealthChanged:Connect(function(NewHP)
	DamageTaken = OldHP - NewHP
	Player.PlayerGui.Healthui.dmgtaken.Text = "DMG Taken: "..math.floor(DamageTaken / 5)
end)

you could change damage taken every time the script notice it is lower then previously:

local Character = script.Parent.Parent.Parent.Parent.Parent
local Humanoid = Character.Humanoid
local Player = game.Players:GetPlayerFromCharacter(Character)
local OldHP = Humanoid.Health
local DamageTaken = 0
local RecentHigh = 0

Humanoid.HealthChanged:Connect(function(NewHP)
	if OldHP < NewHP then return end
    if NewHp < RecentHigh then
       DamageTaken += RecentHigh-NewHp
    end
	RecentHigh = NewHp
	Player.PlayerGui.Healthui.dmgtaken.Text = "DMG Taken: "..math.floor(DamageTaken / 5)
end)
1 Like