Math issue (simple, im just stupid)

local plrHumanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = plrHumanoid.Health

plrHumanoid.HealthChanged:Connect(function(health) --detect if player is injured
	local damage = math.abs(currentHealth - health) --get damage dealt
	game.ReplicatedStorage.RemoteEvents.Bleed:FireServer(damage)
	currentHealth = health --set new currentHealth
end)

this script is detecting if the player’s health has changed, and if it has, fire a remote event
however, i want this remote event to only be fired if the player has taken damage, rather than any health change at all
my idea was something like this

if newHealth < oldHealth then
--fire event
end

but, because im tired or maybe just cuz im dumb i cant figure out if it should be currentHealth < health or currentHealth < damage or health < currentHealth or
yeah
and testing all those things would be complicated and a pain (at least for me)

so yeah, all the variables are there for you, i look forward to the replies in the morning (thank you)

1 Like

My top answer might be to handle damaging the player entirely in its own module or something, so you can easily execute additional callbacks that need to run when ever you’re attempting to deal damage to the player.

However, probably not the most ethical of solutions, just wanted to mention it.

I ran the logic in my head a bit, I think something like this would probably work.

local plrHumanoid = game.Players.LocalPlayer.Character.Humanoid
local oldHealth = plrHumanoid.Health

plrHumanoid.HealthChanged:Connect(function(newHealth) --detect if player is injured
	local damage = newHealth - oldHealth --get damage dealt
	game.ReplicatedStorage.RemoteEvents.Bleed:FireServer(damage)
	currentHealth = health --set new currentHealth
end)

You had it right, just the wrong order. Plus you didn’t need the absolute value. :slight_smile:

1 Like
if currentHealth < heatlh then 
local damg = currentHealth - health
event:FireServer(damg) 
end
currentHealth = health
1 Like

isnt dat his script, u just remove da math.abs() :slight_smile: ?

1 Like

I flipped the operands around, seemed to produce the correct output:

oldHealth = 100

--//Event fires
newHealth = 90

outputHealth = newHealth - 100 >> -- -10, so the player took damage

ye i wasnt notice dat.

s t u p i d w o r d l i m i t

1 Like