I tried to create a script that when a player is low on health (20 health) their walkspeed will decrease and become 5 WalkSpeed. The original Walkspeed of the player is 20, not 16. This is my attempt of scripting it.
if script.Parent.Health >20 then script.Parent.WalkSpeed = 5 wait() if script.Parent.Health <20 then script.Parent.WalkSpeed = 20 end end
I dont know what I'm doing wrong, so some help would be great.
If you want to do script.Parent.Health
, you'll need to place this script in the humanoid of the character. Also in the first line, you're checking to see if the health is Greater
than 20, and on the last line, you're checking to see if the health is less than 20. You have your statements switched up. Simply fix this by:
local function healthChanged(value) if value < 20 then script.Parent.WalkSpeed = 5 else -- You can use else for the opposite of the previous if/then statement script.Parent.WalkSpeed = 20 end end script.Parent.HealthChanged:Connect(healthChanged)--This runs everytime the humanoid's health is changed.
I haven't scripted in a very long time, I might have gotten something wrong. Please correct me if I did.
Let me fix this.
local Humanoid = script.Parent:WaitForChild('Humanoid') local LowSpeed = 5 local NormalSpeed = 20 while wait() do if Humanoid.Health < 20 or Humanoid.Health == 20 then Humanoid.WalkSpeed = LowSpeed else Humanoid.WalkSpeed = NormalSpeed end end
OBS: You will need to put this in a LocalScript inside of StarterCharacterScripts.