How to make a sprint system where you have to press shift to enable it and then press shift again to disable it?

Ive learned how to make a normal sprint system, but usually you stop sprinting once you let go of the button (shift in our topic)
I wanna know how to make it so i dont have to hold the shift to sprint and if i want to stop sprinting i will need to press the button once more

1 Like

Hey there,

Thank you for posting on our forum!

I see that you want to have a sprint toggler/switch using the shift key. I also understand that you have made a prototype/concept where it’s hold to sprint.

I asume you use userinputservice.

Here’s my solution.

You have 1 function that triggers when the player began to press shift, no need for another function to listen when it is being released, in our case.

So, you add a new boolean variable that changes to true when the player is sprinting, and false when it’s not.
You change it to the other state each time you pressed shift:
False > shift pressed > true > shift pressed > false > etc.

Then, after you changed the value of that boolean variable, you check if it’s true.
When it is true, you set the walkspeed to sprinting, else, you change it to normal.

I hope this helps with your needs!

4 Likes

try this

local Players = game:GetService(“Players”)
local UserInputService = game:GetService(“UserInputService”)
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild(“Humanoid”)

local isSprinting = false
local sprintSpeed = 24 – Change this to your sprint speed
local walkSpeed = 16 – Default walk speed

– Update character if it respawns
player.CharacterAdded:Connect(function(char)
character = char
humanoid = char:WaitForChild(“Humanoid”)
end)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
isSprinting = not isSprinting
if humanoid then
humanoid.WalkSpeed = isSprinting and sprintSpeed or walkSpeed
end
end
end)

1 Like
Client only
--LocalScript in StarterPlayer.StarterCharacterScripts

local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local player = Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local sprinting = false
local walkSpeed = hum.WalkSpeed
local sprintSpeed = 24

UIS.InputBegan:Connect(function(input, gpe)
	if gpe or input.KeyCode ~= Enum.KeyCode.LeftShift then return end
	sprinting = not sprinting
	hum.WalkSpeed = sprinting and sprintSpeed or walkSpeed
end)
Client/Server remote
--LocalScript in StarterPlayer.StarterCharacterScripts
task.wait(0.33) --slight pause for the server script to make the remote
--this wait isn't really needed as there is a WaitForChild() in the client script

local UIS = game:GetService("UserInputService")
local Replicated = game:GetService("ReplicatedStorage")

local remote = Replicated:WaitForChild("UGCG")
local sprinting = false

UIS.InputBegan:Connect(function(input, gpe)
	if gpe or input.KeyCode ~= Enum.KeyCode.LeftShift then return end
	sprinting = not sprinting
	remote:FireServer(sprinting)
end)
--ServerScript in ServerScriptService

local Replicated = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--you could make this part manually or not
local remote = Instance.new("RemoteEvent")
remote.Name = "UGCG"
remote.Parent = Replicated

remote.OnServerEvent:Connect(function(plr, sprinting)

	--checking for hackers
	if not Players:GetPlayerFromCharacter(plr.Character) then return end
	if typeof(plr) ~= "Instance" or not plr:IsA("Player") then return end
    --

	local char = plr.Character
	if not char then return end
	local hum = char:FindFirstChildWhichIsA("Humanoid")
	if not hum then return end
	hum.WalkSpeed = sprinting and 24 or 16
end)

Sorry so many edits … had a few errors in testing in the studio.

1 Like

thanks! thats a good idea i havent thought of

2 Likes