MoveDirection not working properly (Sprint script)

I made a Sprint script that requires you to be already moving and then press LShift. Stops the run when your character stops moving, returning to basic WalkSpeed.

Here’s the issue: if you don’t move your mouse while performing the run, it won’t activate (even if you were doing everything right). On the other hand, even slightest mouse movement lets the run work normally.

Basically I’ve noticed the humanoid.MoveDirection detection seems to require mouse movement besides char movement for some reason. It’s what I’m mainly struggling with.

Here’s the code:

    -- Variables
    local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
    local userInputService = game:GetService("UserInputService")
    local vectorZero = Vector3.new(0,0,0)
    local defaultSpeed = 16
    local runSpeed = 26


    -- Run function
    local function detectLShift(input)
    	local runAttempt -- LShift pressed
    	local runInProgress -- Running currently
    	
    	if input.KeyCode == Enum.KeyCode.LeftShift then
    		print("Leftshift pressed")
    		runAttempt = true
    		local function endRunAttempt()
    			if input.KeyCode == Enum.KeyCode.LeftShift then
    				print("runAttempt complete")
    				runAttempt = false -- Ends current runAttempt once input ends
    			end
    		end
    		userInputService.InputEnded:Connect(endRunAttempt)
    	end
    	

    	local function movementCheck()
    		if runAttempt == true and humanoid.MoveDirection ~= vectorZero then
    			print("Run activated")
    			runInProgress = true
    			
    			humanoid.WalkSpeed = runSpeed
    		elseif humanoid.MoveDirection == vectorZero and runInProgress == true then
    			print("Run deactivated")
    			runAttempt = false
    			runInProgress = false
    			
    			humanoid.WalkSpeed = defaultSpeed
    		end
    	end
    	
    	humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(movementCheck)
    end

    userInputService.InputBegan:Connect(detectLShift)

If you’ve encountered this kind of problem, how have you solved this in the past?
Thanks for reading, help means a lot. :slight_smile:

You can try this script. And before you ask on the forum, please do some research on the problem. Also, if you do not know. You need to put it in starter player and make it a local script. The script:

game:GetService(“UserInputService”).InputBegan:connect(function(input,gameprocesed)
if input.KeyCode == Enum.KeyCode.LeftShift then
for i = 1,16 do
wait()
game.Workspace.Camera.FieldOfView = game.Workspace.Camera.FieldOfView + 1.6
game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).WalkSpeed = game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).WalkSpeed + 1
end
end
end)

game:GetService(“UserInputService”).InputEnded:connect(function(input,gameprocesed)
if input.KeyCode == Enum.KeyCode.LeftShift then
for i = 1,16 do
wait()
game.Workspace.Camera.FieldOfView = game.Workspace.Camera.FieldOfView - 1.6
game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).WalkSpeed = game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).WalkSpeed - 1
end
end
end)

game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”).Died:connect(function()
game.Workspace.Camera.FieldOfView = 70
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
script.Disabled = true
end)
(Edit: You can also change the speed.)

One way to fix this would be to listen to the MoveDirection changing, if the magnitude of it is 0, then reset the WalkSpeed.

Now for actually sprinting you could do a quick if statement to see if the MoveDirection is not 0, and then set the WalkSpeed

Example code that wouldn’t work in game but would in theory:

local function ResetSpeed()
	-- set speed back to normal
end

local function Run()
	-- set speed to running 
end

-- Events
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if (Humanoid.MoveDirection.Magnitude == 0) then
		ResetSpeed()
	end
end)

OnInputBegan:Connect(function(io)
	if (io.KeyCode == Enum.KeyCode.LeftShift) then
		-- Check if moving
		if (Humanoid.MoveDirection.Magnitude ~= 0) then 
			Run()
		end
	end
end)

yeah my bad not to include that. Doing it like that already, but may need to search for more possibilities such as statechanged

thanks for the reply bro

thanks, that actually shows a few ways i can optimize stuff. :slight_smile:

appreciate the help friend

1 Like

Np, I am not an advanced scripter. I just did some basics.