Slow player's walkspeed when under 30 hp

Hello developers, I tried using a tutorial to make it so whenever a player’s hp is less than 30 it slows them down to 10 walkspeed (default walkspeed is 16) but I also have a stamina system which is basically shift to sprint. (changes 16 walkspeed to 21 for a certain amount of time) So the problem is just that whenever your player’s hp is over 30 its set to 16 but when you use the stamina system its still set to 16 when its supposed to be 21 because of the slow down script.

Here’s the slow down walkspeed local script:

local humanoid = script.Parent:FindFirstChild("Humanoid")

while true do
	wait ()
	if humanoid.Health <= 30 then
		humanoid.WalkSpeed = 10
	elseif	
		humanoid.Health >= 30 then
		humanoid.WalkSpeed = 16
	end
end

And here’s my stamina local script:

local player = game.Players.LocalPlayer
local character = player.character

local UserInputService = game:GetService("UserInputService")

local stamina = 100
local running = false

stamina = math.clamp(stamina, 0,100)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = true
		character.Humanoid.WalkSpeed = 21
		while stamina > 0 and running do
			stamina = stamina - 1
			script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 1, 0), "Out", "Linear", 0)
			wait()
			if stamina == 0 then
				character.Humanoid.WalkSpeed = 16
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = false
		character.Humanoid.WalkSpeed = 16
		while stamina < 100 and not running do
			stamina = stamina + 1
			script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 1, 0), "Out", "Linear", 0)
			wait()
			if stamina == 0 then
				character.Humanoid.WalkSpeed = 16
			end
		end
	end
end)

Thanks!

Firstly, instead of having the loop to check the health and setting the walkspeed, you can use the Humanoid.HealthChanged event so that way it only runs when health as lowered or increased.

Secondly, I think it may be best to put the stamina system and the walkspeed script in the same script to make it easier to fix the issue.

A fix I would do is when you’re sprinting and your health goes above or below 30, check the running boolean, if it’s true, make it set the speed to 21, if it’s false, make it 10 or 16 depending on the health.

And whe nyou stop running, check if health below or equal to 30, if it is, set the speed to 10, and if it isn’t set it to 16

Should look something like this given the information you gave

local player = game.Players.LocalPlayer
local character = player.Character

local UserInputService = game:GetService("UserInputService")

local stamina = 100
local running = false

stamina = math.clamp(stamina, 0,100)

local humanoid = character:FindFirstChild("Humanoid") or character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = true
		humanoid.WalkSpeed = 21
		while stamina > 0 and running do
			stamina -= 1
			script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 1, 0), "Out", "Linear", 0)
			task.wait()
			if stamina == 0 then
				humanoid.WalkSpeed = 16
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = false
		humanoid.WalkSpeed = if humanoid.Health <= 30 then 10 else 16
		while stamina < 100 and not running do
			stamina += 1
			script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 1, 0), "Out", "Linear", 0)
			task.wait()
		end
	end
end)

humanoid.HealthChanged:Connect(function(newHealth)
	if newHealth <= 30 then
		humanoid.WalkSpeed = if running then 21 else 10
	else
		humanoid.WalkSpeed = if running then 21 else 16
	end
end)

Haven’t tested the code but it should work, if it doesn’t I probably made a mistake on my end

3 Likes

The slow player part works but the stamina part doesn’t quite work so basically it slows the player down when the player’s under 30 hp but if you use the stamina the walkspeed is instantly 21 again is there a way to make it 10 when your hp is under 30 and when your sprinting its set to 15 for the certain amount of time then it goes back to 10 when the stamina is on cooldown

In that case, I think you can change

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = true
		humanoid.WalkSpeed = 21
		while stamina > 0 and running do
			stamina -= 1
			script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 1, 0), "Out", "Linear", 0)
			task.wait()
			if stamina == 0 then
				humanoid.WalkSpeed = 16
			end
		end
	end
end)

To this

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		running = true
		humanoid.WalkSpeed = if humanoid.Health <= 30 then 15 else 21
		while stamina > 0 and running do
			stamina -= 1
			script.Parent:TweenSize(UDim2.new(stamina / 100, 0, 1, 0), "Out", "Linear", 0)
			task.wait()
			if stamina == 0 then
				humanoid.WalkSpeed = if humanoid.Health <= 30 then 10 else 16
			end
		end
	end
end)

@R4gingNoob

1 Like

Thank you very much it works perfect!
(I gave you solution)

2 Likes