Changing the player's walkspeed based on health

I have a function that changes a player’s walkspeed based on their health, but it only changes their walkspeed when their health is less than 75, not when it is lower than 50. I’m not sure why this is happening.
Edit: I think it might have something to do with my stun system script. Here’s my stun system script and the movement script:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		
		local oldwalkspeed = humanoid.WalkSpeed
		local oldjumpPower = humanoid.JumpPower

		humanoid.ChildAdded:Connect(function(child)
			if child.Name == "StunEffect" then
				
				game.ReplicatedStorage.StunStart:FireClient(player)
				
				humanoid.WalkSpeed = 0
				humanoid.JumpPower = 0
			end
		end)

		humanoid.ChildRemoved:Connect(function(childremoved)
			if humanoid:FindFirstChild("StunEffect") then
				humanoid.WalkSpeed = 0
				humanoid.JumpPower = 0
			else
				
				game.ReplicatedStorage.StunFinish:FireClient(player)
				
				humanoid.WalkSpeed = oldwalkspeed
				humanoid.JumpPower = oldjumpPower
			end
		end)
	end)
end)
----------------------------------------------------------------------------------------------------------------------------------------
local debounce = false
local ss = game.ReplicatedStorage:WaitForChild("StunStart")
local sf = game.ReplicatedStorage:WaitForChild("StunFinish")

local function cd()
	debounce = true
end
ss.OnServerEvent:Connect(cd)

local function offcd()
	debounce = false
end
sf.OnServerEvent:Connect(offcd)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		humanoid.WalkSpeed = 30

		humanoid.HealthChanged:Connect(function()
			if debounce then return end
			debounce = true
			if humanoid.Health <= 75 and humanoid.Health > 50 then
				humanoid.WalkSpeed = 16
			end 
			if humanoid.Health <= 50 then
				humanoid.WalkSpeed = 5
			end
		end)
	end)
end)
3 Likes

hmm…

Maybe it is because you dont set the debounce back to false?

2 Likes

Try this out:

humanoid.HealthChanged:Connect(function()
	if debounce then return end
	debounce = true
	
	if humanoid.Health <= 75 and humanoid.Health > 50 then
		humanoid.WalkSpeed = 16
	elseif humanoid.Health <= 50 then
		humanoid.WalkSpeed = 5
	end
end)

Better for code formatting but I don’t see how this would change the behavior.

2 Likes

Try printing ‘humanoid.Healht’ and adding prints inside the if statements.