I'm trying to make a script that makes a player slow down when damaged. So, if my max health is 100 and my health is 50 and my speed is 16, my walkspeed should become 8. This is the code:
game.Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function(character) repeat wait() until character.Humanoid local humanoid = character.Humanoid local originalSpeed = humanoid.WalkSpeed humanoid:GetPropertyChangedSignal("Health"):Connect(function() print("health changed") humanoid.WalkSpeed = humanoid.Health / humanoid.MaxHealth * originalSpeed print("walkspeed updated:", humanoid.WalkSpeed) end) end) end)
It works perfectly fine, until this happens:
health changed walkspeed updated: 8 health changed walkspeed updated: 8.1613359451294 health changed walkspeed updated: 8.3214712142944 health changed walkspeed updated: 8.4838285446167 health changed walkspeed updated: 8.6445035934448
why does this happen, and how can i fix it?
I think. its probably because of Roblox's auto health regeneration. When a player is damaged, the health will regenerate over time .So that's why your script is firing many times, it because the health keeps changing (increasing/regenerating).