How to detect when you get damaged

Smart. Forgot to do that, mind has been all over the place recently lol. Been very sick and it’s thrown off my whole thinking process :sneezing_face:

1 Like

ummmmmmmmm I fixed the symbol and it still only does whatevae I want when i die

try this (it wasent made by me):

local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local OldHealth = Humanoid.Health

Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
if Humanoid.Health < OldHealth then
	print("damage taken")
end

OldHealth = Humanoid.Health
end)

(also, this should be in a local script in startercharacterscripts. if you werent looking for a client sided way of doing so or this doesn’t work please let me know and i’ll try my best to help. feel free to manipulate this code in any way)

The damageTaken variable is there for you to reference in your own code, try adding print(damageTaken)

you stole my reply >:(

This text will be blurred[/spoiler][spoiler]This text will be blurred

im get error taht say attempt to index number with ‘GetPropertyChangedSignal’

Try this code:

local Players = game:GetService("Players")
local LPlayer = Players.LocalPlayer
local character = game.Workspace:WaitForChild(LPlayer.Name) or LPlayer.Character or LPlayer.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local OldHealth = Humanoid.Health

wait(1)

Humanoid.Health.HealthChanged:Connect(function(health)
    if Humanoid.Health < OldHealth then
        print("DamageTaken")
        - - whatever you want to do here
    end
end)

ummmm that not work the script im using is inside of a tool by the way im dont know if that changed anything

the script only works in startercharacterscripts and should be a local script

hi :wave: :wave: :wave: :wave: :wave: :wave: :wave: :wave: Im meant to say script inside of charcater im mistake

hhhhhhi this not work im need help

Do this:

local Players = game:GetService("Players")
local LPlayer = Players.LocalPlayer
local character = game.Workspace:WaitForChild(LPlayer.Name) or LPlayer.Character or LPlayer.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local OldHealth = Humanoid.Health
local LatestDamage = "0"
task.wait(1)

if Humanoid then
	Humanoid.HealthChanged:Connect(function(h)
		if h < OldHealth then
			local damage = (Humanoid.MaxHealth - h)
			print("Damage was taken! (" .. tostring(damage) .. ", Current health: " .. tostring(h))
			LatestDamage = tostring(damage)
		end
        OldHealth = Humanoid.Health
	end)
end

Should work

the script only works when my character dies (im tested this using TakeDamge() on my humanoid) an this keeps happening with a few scripttt im try that kind of weird

I edited the script to fix a line, I’ll upload the new one again:

local Players = game:GetService("Players")
local LPlayer = Players.LocalPlayer
local character = game.Workspace:WaitForChild(LPlayer.Name) or LPlayer.Character or LPlayer.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local OldHealth = Humanoid.Health
local LatestDamage = "0"
task.wait(1)

if Humanoid then
	Humanoid.HealthChanged:Connect(function(h)
		if h < OldHealth then
			local damage = (Humanoid.MaxHealth - h)
			print("Damage was taken! (" .. tostring(damage) .. ", Current health: " .. tostring(h))
			LatestDamage = tostring(damage)
		end
        OldHealth = Humanoid.Health
	end)
end

ermmmmmmm that also only works when I die (I have 3000 max hp)

You are running :TakeDamage() on the command line; which will run the code on Client Side.

You are running this script on the Server side, server cannot view what the client runs unless done with remote events.

You can press F9 or type /console. Then, press “Server”, and in the command line, run the takedamage script again.

I ran it on the server side, this is what it returned each time I took 10 damage:

You can use math.round if you don’t want a long decimal number.

1 Like

woahhhhhhhhhh it worksss only problem is that each time I take damage the last amount of damage is added to it (im explain poorly Im think) so its not really the amount of damage i toooook

edit: hi

Ah, try this formula:

local Players = game:GetService("Players")
local LPlayer = Players.LocalPlayer
local character = game.Workspace:WaitForChild(LPlayer.Name) or LPlayer.Character or LPlayer.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local OldHealth = Humanoid.Health
local LatestDamage = "0"
local Health = Humanoid.Health
task.wait(1)

if Humanoid then
	Humanoid.HealthChanged:Connect(function(h)
		if h < OldHealth then
			local damage = (Health - h)
			print("Damage was taken! (" .. tostring(damage) .. ", Current health: " .. tostring(h))
			LatestDamage = tostring(damage)
		end
        OldHealth = h
	end)
end

(Health - h)

You don’t need if Humanoid then since the code will infinitely yield at character:WaitForChild("Humanoid") if an instance named ‘Humanoid’ doesn’t exist inside the player’s character.