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)
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)
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.