Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

3 comments

0

Where is the script placed into?

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

2 answers

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.