Sprint Script Not Working

So I’m trying to get this sprint script to work that makes the player sprint or walk and increases their FOV when sprinting.

For some reason it will not work and everything seems fine. I’ve tried looking at other DevForum posts about this topic and nothing works for some reason. My script is in StarterPlayer Scripts because it will error anywhere else. Thank you.

Here is the sprint script:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local UserInputService = game:GetService("UserInputService")

-- Variables
local defaultWalkSpeed = humanoid.WalkSpeed
local sprintWalkSpeed = 20 
local defaultFOV = 70
local sprintFOV = 80  

-- Function to handle sprinting
local function handleSprint()
    if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then
        humanoid.WalkSpeed = sprintWalkSpeed
        game:GetService("Workspace").CurrentCamera.FieldOfView = sprintFOV
    else
        humanoid.WalkSpeed = defaultWalkSpeed
        game:GetService("Workspace").CurrentCamera.FieldOfView = defaultFOV
    end
end

-- Connect the function to the RenderStepped event
game:GetService("RunService").RenderStepped:Connect(handleSprint)

Try to print anything after and before the if usi:keydown part) and print anythinf after else and tell me the result

Im not 100% but LeftShift may be invalid. Its worth checking the documentation

Well if it prints anything except the print before usi:iskeydowm then i might figure it out

It won’t print anything, maybe has to do with the actual function being called?

Even before usi:iskeydown?u sure?

Before and after nothing will print

game:GetService(“RunService”).RenderStepped:Connect(function)

handleSprint()
print(“worked here”)
end)

Try what i sent u above and tell me if it works

This script should work to achieve what you’re looking for (LocalScript in StarterCharacterScripts):

local Workspace = game:GetService("Workspace")
local ContextActionService = game:GetService("ContextActionService")

local SPRINT_WALK_SPEED = 20
local SPRINT_FOV = 80

local humanoid = script.Parent:WaitForChild("Humanoid")
local defaultWalkSpeed = humanoid.WalkSpeed

local currentCamera = Workspace.CurrentCamera or Workspace:WaitForChild("Camera")
local defaultFOV = currentCamera.FieldOfView

local function onSprint(_, inputState)
	if inputState == Enum.UserInputState.Begin then
		humanoid.WalkSpeed = SPRINT_WALK_SPEED
		currentCamera.FieldOfView = SPRINT_FOV
	else
		humanoid.WalkSpeed = defaultWalkSpeed
		currentCamera.FieldOfView = defaultFOV
	end
end

ContextActionService:BindAction("Sprint", onSprint, false, Enum.KeyCode.LeftShift)

Nothing prints. I put it at the end of the script.

Send me a pic about the location of ur script please

If the script is a local script will it still be seen by other players that you are sprinting? Because it works with my original with local but I’m not sure if it would work for other players to see, or for the server to see.

Yes because since players are network owners of their character, it will replicate for other players correctly

Alright thank you it works now with local script.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.