Code Review: ShiftToRun LocalScript

Hi,
I made a LocalScript that allows the player to sprint by holding the Shift key and walk again when releasing it.
It’s a simple functionality, but I’d like to know how I can improve my code.
Are there any more efficient approaches, cleaner structures, or best practices I should consider?
Thanks in advance for your feedback! :blush:

My code:

--------------------------------------------
-- //: Variables
local ts = game:GetService("TweenService")
local tweenDuration = 1

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local animator = hum:FindFirstChild("Animator")

local uis = game:GetService("UserInputService")
local runKey = Enum.KeyCode.LeftShift
local runningSpeed = 20
local walkSpeed = 16

local currentCam = workspace.CurrentCamera
local zoomOutValue = 90
local normalZoomValue = 70

local runAnim = game:GetService("ReplicatedStorage").Assets.Animations.RunAnimation
local track = animator:LoadAnimation(runAnim)

local holdingW = false -- Tracks if the player is holding the 'W' key; ensure running only starts when moving forward.
--------------------------------------------




--------------------------------------------
-- //: Functions
local function zoomOut()
	ts:Create(currentCam, TweenInfo.new(tweenDuration), {FieldOfView = zoomOutValue}):Play()
end

local function zoomNormal()
	ts:Create(currentCam, TweenInfo.new(tweenDuration), {FieldOfView = normalZoomValue}):Play()
end


local function startRunning()
	zoomOut()
	hum.WalkSpeed = runningSpeed
	track:Play()
end

local function startWalking()
	zoomNormal()
	hum.WalkSpeed = walkSpeed
	track:Stop()
end


-- When you hold the W key:
uis.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end
	holdingW = true
end)

-- When you release the W key:
uis.InputEnded:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end
	holdingW = false
	
	startWalking()
end)
--------------------------------------------




--------------------------------------------
-- //: Main Function

-- If you hold shift, you run:
uis.InputBegan:Connect(function(input, gameProcessed)
	
	if gameProcessed then return end
	if input.KeyCode ~= runKey then return end
	if holdingW == false then return end
	
	startRunning()
	
end)

-- If you release shift, you walk:
uis.InputEnded:Connect(function(input, gameProcessed)

	if gameProcessed then return end
	if input.KeyCode ~= runKey then return end
	
	startWalking()
	
end)
--------------------------------------------

Video:

The only thing I would suggest for you to change is the check for if the w key is held down. When Checking if the player is holding shift you can simply add an if statement that says if usi:IskeyDown(Enum.KeyCode.W) then

1 Like

Bro thank you so much,

I didn’t know about the :IsKeyDown() method.
Bro this method is sooo nice.

Now I don’t have to use the variable “holdingW”

I appriciate it extremly much man.

Also Thanks for reading my code.

To make this work on mobile you can check the humanoids Move Direction

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