How do i make hold a button to sprint but instead of player instanly gain a speed, he would accelarate

i cannot understand how do i make it and make it work right

mostly what i need to know is how to make:
Player that holds button to sprint not moving, not accelarate but right after they start to walk to then start sprinting and accelarate
and
how to make:
Player lose all of the speed he gained after he stopped even when he still holding a button

Try this

local WALK_SPEED = 16
local SPRINT_SPEED = 24
local SPEED_CHANGE_SPEED = 5

local isSprintKeyDown --Use UserInputService to define it
local character --Player.Character

game:GetService(“RunService”).Heartbeat:Connect(function(deltaTime)
 local mult = isSprintKeyDown and 1 or -1
 character.Humanoid.WalkSpeed = math.clamp(character.Humanoid.WalkSpeed + deltaTime * mult * SPEED_CHANGE_SPEED, WALK_SPEED, SPRINT_SPEED)
 if character.Humanoid.MoveDirection == Vector3.zero then
  character.Humanoid.WalkSpeed = WALK_SPEED
 end
end)
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

local walkingSpeed = 16
local sprintSpeed = 50
local accelerationTime = 1.5
local isSprinting = false
local speed = walkingSpeed

UIS.InputBegan:Connect(function(input, processed)
    if input.KeyCode == Enum.KeyCode.LeftShift and not processed then
        isSprinting = true
        while isSprinting do
            speed = math.min(sprintSpeed, speed + (sprintSpeed - walkingSpeed) / (accelerationTime * 60))
            humanoid.WalkSpeed = speed
            task.wait(1/60)
        end
    end
end)

UIS.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        isSprinting = false
        while speed > walkingSpeed do
            speed = math.max(walkingSpeed, speed - (sprintSpeed - walkingSpeed) / (accelerationTime * 60))
            humanoid.WalkSpeed = speed
            task.wait(1/60)
        end
    end
end)
1 Like

Just use TweenService to tween the walkspeed.

Here is an example. Put this inside of StarterPlayerScripts.

local UserInputService = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local DefaultSpeed = Humanoid.WalkSpeed

local Speed = 35
local SpeedDelay = 0.05

UserInputService.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.LeftShift and Humanoid then
		local LastSpeed
		while Humanoid.WalkSpeed < Speed do
			LastSpeed = Humanoid.WalkSpeed
			Humanoid.WalkSpeed = LastSpeed + 1
			task.wait(SpeedDelay)
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = DefaultSpeed
	end
end)

Humanoid.Died:Connect(function()
	Character = Player.CharacterAdded:Wait()
	Humanoid = Character:WaitForChild("Humanoid")
end)

okay let me try them all

and i will also try tweenservice

they all great but still didnt answered first question

still thanks