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.

1if script.Parent.Health >20 then
2    script.Parent.WalkSpeed = 5
3    wait()
4    if script.Parent.Health <20 then
5        script.Parent.WalkSpeed = 20
6    end
7end

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:

1local function healthChanged(value)
2    if value < 20 then
3        script.Parent.WalkSpeed = 5
4    else -- You can use else for the opposite of the previous if/then statement
5        script.Parent.WalkSpeed = 20
6    end
7end
8 
9script.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 4 years ago

Let me fix this.

01local Humanoid = script.Parent:WaitForChild('Humanoid')
02 
03 
04local LowSpeed = 5
05 
06local NormalSpeed = 20
07 
08 
09while wait() do
10 
11 
12    if Humanoid.Health < 20 or Humanoid.Health == 20 then
13 
14 
15        Humanoid.WalkSpeed = LowSpeed
View all 27 lines...

OBS: You will need to put this in a LocalScript inside of StarterCharacterScripts.

Answer this question