Stamina and FOV still changes when i hold shift and stop moving

Hello! I need help and I’m kinda noob at scripting. I tried to make a run and crouch script but the problem is the stamina still decreases and the fov still changes when I hold shift even if I’m not moving at all.

Here’s the code:

UIS.InputBegan:connect(function(key, gameProcessed)
	if character.Humanoid.Health >= 26 then
		if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
			if Humanoid.MoveDirection.Magnitude > 0 then
				if sprinting == false and crouch == false then
					sprinting = true
					crouch = false
					while power > 0 and sprinting do
						character.Humanoid.WalkSpeed = NewWalkSpeed				
						power = power - .1
						Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
						fovin:Play()
						wait()
						if power <= 0 then
							character.Humanoid.WalkSpeed = NormalWalkSpeed
						else
						end
					end
				end
			end
		end
	end
end)



UIS.InputEnded:connect(function(key, gameProcessed)
	if character.Humanoid.Health >= 26 then
		if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
			if sprinting == true and crouch == false then
				sprinting = false
				crouch = false
				while power < 10 and not sprinting do
					character.Humanoid.WalkSpeed = NormalWalkSpeed
					fovout:Play()
					power = power + 0.03
					Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
					wait()
					if power <= 0 then
						character.Humanoid.WalkSpeed = NormalWalkSpeed
					end
				end
			end
		end
	end
end)

Video: Link

Hope someone can help. Thank you!

2 Likes

Hi. I’m currently on mobile so it’s kinda hard to help but as I saw on the script there is no speed check for humanoid what u can do is check player velocity/speed on a new function and send it to a global value

example after getting the value u can add
if speed > 0 then end / if speed == 0 then return end
what this does is if player speed bigger than 0 (if player is moving) then enable the function.

2 Likes

it still sadly didn’t work. :frowning:

1 Like

Hmm… did u get the player speed from humanoid or from the body parts ?

1 Like

Try putting the FOV tween thing before while power > 0 and sprinting do.

sprinting = true
crouch = false
fovin:Play()
sprinting = false
crouch = false
fovout:Play()

I believe, like this, the FOV will change when the player is just holding the sprint key in general.

1 Like

I can help you because i had same problem (p.s. i used same script and lol i remember it very well)

RunService.Heartbeat:Connect(function() -- connect heartbeat
 if sprinting then -- check if running
  if Humanoid.MoveDirection.Magnitude <= 0 then -- if player not moving then
   character.Humanoid.WalkSpeed = NormalWalkSpeed -- set player speed
   fovin:Cancel() -- canceling tween
   fovout:Play() -- playing tween
  end
 end
end)

2 Likes

hello! sorry for replying very late. but I tried to do this and now it wont let me run

script:

UIS.InputBegan:Connect(function(k,gp)
	if k.KeyCode == Enum.KeyCode.LeftShift and gp == false then
		sprinting = true
	elseif k.KeyCode == Enum.KeyCode.C and gp == false then
		player.CameraMode = Enum.CameraMode.Classic
		LoadCrouchAnimation:Play()
		crouch = true
	end
end)

UIS.InputEnded:Connect(function(k,gp)
	if k.KeyCode == Enum.KeyCode.LeftShift and gp == false then
		sprinting = false
	elseif k.KeyCode == Enum.KeyCode.C and gp == false then
		player.CameraMode = Enum.CameraMode.LockFirstPerson
		LoadCrouchAnimation:Stop()
		crouch = false
	end
end)

--[actual script]--

game:GetService("RunService").Heartbeat:Connect(function()
	if sprinting == true and power > 0 then
		if Humanoid.MoveDirection.Magnitude > 0 then
			Humanoid.WalkSpeed = NewWalkSpeed
			fovout:Cancel()
			fovin:Play()
			power = power - .1
			Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
		end
	elseif sprinting == true and power < 0 then
		sprinting = false
	end
	
	if sprinting == false then
		character.Humanoid.WalkSpeed = NormalWalkSpeed
		fovin:Cancel()
		fovout:Play()
		power = power + 0.03
		Bar:TweenSize(UDim2.new(power / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
	end
	
	if crouch == true then
		Humanoid.JumpPower = 0
		Humanoid.WalkSpeed = 5
		if Humanoid.MoveDirection.Magnitude > 0 then
			LoadCrouchAnimation:AdjustSpeed(0.75)
		else
			LoadCrouchAnimation:AdjustSpeed(0)
		end
	end
	
	if crouch == false then
		crouch = false
		sprinting = false
		Humanoid.JumpPower = 50
		Humanoid.WalkSpeed = 14
	end
end)

the crouch works but the run would not

i tried to do this:
image

and this is what I got: