Damage Script not working

My Aim: To create a script where when a player is damage it is printed

My Problem: My code isn’t giving any output not even an error

I Have Tried: I have look at the Roblox Humanoid HealthChanged artical but that didn’t help me

Script in ServerScriptService

local LastHealth

local function OnDamage(NewHeath)
	local HealthChange = LastHealth + NewHeath
	
	LastHealth = NewHeath
	
	if HealthChange > 0 then
		print(HealthChange)
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
		
		LastHealth = Humanoid.Health
		
		Humanoid.HealthChanged:Connect(OnDamage)
	end)
end)

HealthChange will never be lower than 0 since you’re adding the 2 results together, and NewHealth will always be positive unless below 0. The parameter returns the players new health, but I believe you’ve just got the equation wrong. To figure out the health change it should be LastHealth - NewHeath.

1 Like

Just do something like this:

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local Humanoid = Character:WaitForChild("Humanoid")
        local CurrentHealth = Humanoid.Health

        Humanoid.HealthChanged:Connect(function(NewHealth)) 
            local Change = math.abs(CurrentHealth - NewHealth)
            local Damage = CurrentHealth > NewHealth --This is actually a Bool Variable

            if Damage then
                print("The player took: "..Change.." damage")
            end

            CurrentHealth = NewHealth
        end)
    end)
end)

You don’t need to reference a local function, you could just do this all inside encased functions to make it easier

1 Like

HealthChange will trigger if the player is healing

It took one quick chane on line 6 (double brackets)