How to make slow speed gain until max speed

Hi,

I want to make my game realistic by making running start slow first then gain momentum until they reach the max speed. How would I do this?

1 Like

Do you have a running script already or are you starting from zero?

1 Like

If you’re starting from zero, you could do something like this:

local UIS = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

UIS.InputBegan:Connect(function(input, processed)
	if processed then -- Check if any GUI item is selected.
		return -- Return if that's the case.
	end
	
	if input.KeyCode ~= Enum.KeyCode.LeftShift and input.KeyCode ~= Enum.KeyCode.RightShift then
		return -- Return if not pressing the shift key.
	end
	
	-- To take some time before running faster.
	repeat task.wait(.2)
		Humanoid.WalkSpeed += 1
	until Humanoid.WalkSpeed == 30
end)

UIS.InputEnded:Connect(function(input, processed)
	if processed then -- Check if any GUI item is selected.
		return -- Return if that's the case.
	end
	
	if input.KeyCode ~= Enum.KeyCode.LeftShift and input.KeyCode ~= Enum.KeyCode.RightShift then
		return -- Return if not pressing the shift key.
	end
	
	-- To slow down faster
	repeat task.wait(.1)
		Humanoid.WalkSpeed -= 2
	until Humanoid.WalkSpeed == 16
end)

Make it a LocalScript and parent it to StarterCharacterScripts. I commented the script so you understand what it does.

1 Like

you could do this.

task.wait(.5)

-- // variables \\ --

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local hum = char:WaitForChild("Humanoid")

local run = game:GetService("RunService")
local uis = game:GetService("UserInputService")

local walkSpeed = 16
local sprintSpeed = 25

local iterateSpeed = 0.07

local sprintingKey = Enum.KeyCode.LeftShift

local chosenSpeed = nil

---------------------

-- // functions \\ --

local function lerp(a, b, t)
	return a + (b - a) * t
end

local function keyPressed(key, proc)
	if not proc then
		if key.KeyCode == sprintingKey then
			chosenSpeed = sprintSpeed
		end
	end
end

local function keyLetGo(key, proc)
	if not proc then
		if key.KeyCode == sprintingKey then
			chosenSpeed = walkSpeed
		end
	end
end

--------------------

uis.InputBegan:Connect(keyPressed)
uis.InputEnded:Connect(keyLetGo)

run.RenderStepped:Connect(function()
	if chosenSpeed and hum.WalkSpeed ~= chosenSpeed then
		hum.WalkSpeed = lerp(hum.WalkSpeed, chosenSpeed, iterateSpeed)	
	end
end)
1 Like

Starting from zero

Lorem Ipsum

Is it possible to make this mobile compatible???

I’ve never personally tried anything mobile, and honestly the only way I can think of mobile would be CAS (ContextActionService). I don’t really know if I can make something that work’s because I only use UIS (UserInputService), and CAS rewrites already made inputs; for example setting a key to W will unbind W’s original function which is to move forward and instead change it to whatever you set it as in the function you provided.

1 Like

Ok, I’ll test this out. Thank you

I would set the WalkSpeed to 0, and then use a Tween.

Humanoid.WalkSpeed = 0
local Gain = game:GetService('TweenService'):Create(Humanoid, TweenInfo.new(1), {WalkSpeed = 32}) 
Gain:Play()

do you want jumping to add/decrease velocity since you are making it realistic or no

by that i mean no walkspeed increase while jumping or no (first one is more realistic)

I wasn’t really looking for those types of solutions but I came up with default code so I can try to help you guys understand what I am trying to do.

-- controls movement animation
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera

local Figure = LocalPlayer.Character
local Humanoid = Figure:WaitForChild("Humanoid")

local scaleDampening = Figure:WaitForChild("Animate"):WaitForChild("ScaleDampeningPercent")
local defaultDampening = scaleDampening.Value

function OnDirectionChange()
	local RelativeDirection = Humanoid.MoveDirection:Dot(Camera.CFrame.LookVector)
	
	-- my attempt in solving animation issue
	local speedFactor = Humanoid.WalkSpeed / 16 
	scaleDampening.Value = defaultDampening / speedFactor
	
	if RelativeDirection > 0.75 then -- foward
		print("Moving Foward")
		Humanoid.WalkSpeed = 10
        -- write some code here that will help regain speed
	end
	
	if RelativeDirection < -0.75 then -- foward
		print("Moving Backwards")
		Humanoid.WalkSpeed = 6
	end
end

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(OnDirectionChange)

I would help but you have to answer the question i asked for it to yk be possible for m e to help

I found the solution:

function OnDirectionChange()
	local RelativeDirection = Humanoid.MoveDirection:Dot(Camera.CFrame.LookVector)

	if RelativeDirection > 0.68 or RelativeDirection > 0.75 then -- foward
		if not WalkLerp then
			WalkLerp = RunService.RenderStepped:Connect(function()				
				Humanoid.WalkSpeed = lerp(Humanoid.WalkSpeed, MaxSpeed, 0.4)
			end)
		end	
	end

	if RelativeDirection < -0.68 or RelativeDirection < -0.75 then
		print("Backwards")
		Humanoid.WalkSpeed = 6
		if WalkLerp then
			print("Disconnected")
			WalkLerp:Disconnect()
			WalkLerp = nil
		end
	end

	if RelativeDirection == 0 then -- a or d or standing still
		print("Sides")
		Humanoid.WalkSpeed = 10
		if WalkLerp then
			print("Disconnected")
			WalkLerp:Disconnect()
			WalkLerp = nil
		end
	end
end

Thanks to @Soulx_xFlame’s original post, I came up with this solution. Works for mobile too.

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