Scripts does weird value changes

I’m trying to make it so, a bit of taken damage goes back to one of player limbs. It does work at a first point, however then it is start doing weird stuff. Incase of taken well high damage just loop the part of going down. If humanoid takes a little damage then it is still going down as supposed to, however then it loops and going up.

player.CharacterAdded:Connect(function(character)
        local hum = character.Humanoid
        local dmgtakentorso
        hum.HealthChanged:Connect(function(newhealth)
            dmgtakentorso = (hum.MaxHealth - newhealth) - 4
            torso.Value = torso.Value - dmgtakentorso
        end)
    end)

im pretty sure you want the value to change only when the health goes down. if that is so, follow these steps, because i think ik the problem.

  1. create a variable called “lastHealth” and put it right below the “hum” variable.
  2. set that “lastHealth” variable you just added to the humanoid’s health
  3. in your HealthChanged event, add this if statement on the very top:
    if not hum.Health < lastHealth then return end
  4. then on the bottom of the HealthChanged event, put this code:
    lastHealth = hum.Health

you should have this as your code:

player.CharacterAdded:Connect(function(character)
	local hum = character.Humanoid
	local lastHealth = hum.Health
	local dmgtakentorso
	hum.HealthChanged:Connect(function(newhealth)
		if not hum.Health < lastHealth then return end
		dmgtakentorso = (hum.MaxHealth - newhealth) - 4
		torso.Value = torso.Value - dmgtakentorso
		lastHealth = hum.Health
	end)
end)

i hope this works

image

ok fixed it. the code should be this. i don’t have enough time to explain it rn

player.CharacterAdded:Connect(function(character)
	local hum = character.Humanoid
	local lastHealth = hum.Health
	local dmgtakentorso
	hum.HealthChanged:Connect(function(newhealth)
		if hum.Health < lastHealth then
             dmgtakentorso = (hum.MaxHealth - newhealth) - 4
		     torso.Value = torso.Value - dmgtakentorso
		     lastHealth = hum.Health
        end
	end)
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.