Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to slow down a plr when they are low on health?

Asked by 5 years ago

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.

0
Where is the script placed into? tonicos33 6 — 5y
0
dont nest an if statement in there, the nested if statement isnt going to run in this instance, and a number cant be both greater than, and less than 20 theking48989987 2147 — 5y

2 answers

Log in to vote
0
Answered by
blockmask 374 Moderation Voter
5 years ago

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.

Ad
Log in to vote
0
Answered by 3 years ago

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.

Answer this question