How To Detect Damage Taken

I’m trying to make a system where you take knockback when hurt, but not so sure how to detect damage.
So far all I have is

	hit.Parent.Torso.Velocity=door.CFrame.lookVector * -5

Does anybody know how to detect damage? If so please tell me how. (BTW I’m kind of bad at lua)

4 Likes

I’m not that good at Lua either, I just started. But I will try to help.
I think you can do a local script and store the value of the player’s health in a variable. Then in a forever loop, check if the player’s health is lower than the last saved health (then do knockback). else set the variable to the current player health.
But I think it might be better to tell the player somehow, maybe through remote events whenever something damages the player (through the damage script)

https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

1 Like

Using the other response i found out how to do it. But I don’t know if this script would be local or not or where it would go? Do you know?

local humanoid = game.Players.LocalPlayer.Character.Humanoid

local currentHealth = humanoid.Health

humanoid.HealthChanged:Connect(function(health)

local change = math.abs(currentHealth - health)

hit.Parent.Torso.Velocity=door.CFrame.lookVector * -1000

currentHealth = health

end)
1 Like

probably local script because of

the “localPlayer” I am pretty sure that you can only use that in local script

1 Like