Add Acceleration to the Player's Speed?

How would I construct a mechanic that has the player slower increases speed when they start moving and have it decreases when they stop moving?

8 Likes

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded():Wait()
local Humanoid = Character.Humanoid
local Root = Character.HumanoidRootPart

local BaseWaitTimer = .4
local MinSpeed = 12
local MaxSpeed = 40


local WaitTimer = BaseWaitTimer
Humanoid.WalkSpeed = MinSpeed

function Start()
	
	local MoveDirDB = false
	
	local function Accelerate()
		if Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) and MoveDirDB == false and Humanoid.WalkSpeed < MaxSpeed then
			MoveDirDB = true
			while Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) and Humanoid.WalkSpeed < MaxSpeed do
				Humanoid.WalkSpeed = Humanoid.WalkSpeed + 1
				
				wait(WaitTimer)
				WaitTimer = WaitTimer / 1.1
			end
			MoveDirDB = false
		elseif Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
			WaitTimer = BaseWaitTimer
			Humanoid.WalkSpeed = MinSpeed			
		end
	end
	
	Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(Accelerate)
end

Start()

this is an Acceleration local script I made, Adding Acceleration is rather easy but Deceleration is an other matter and I can’t really think of a good way to do it currently

34 Likes

I never said it was not i was just giving him another easier method he could use.

Oh, I was confused because you replied to me and not him

Did i really XD my bad ill fix that

1 Like

I assume you want this mechanic to be for pc/mobile/xbox compatible so your best option is to use Humanoid.StateChanged and do a while loop to check if the Humanoids state is still running if it is then increase the speed. Ex?:

local IsRunning = Instance.new(“BoolValue”,plr)

IsRunning.Value = false

IsRunning.Changed:Connect(function(newval)
if newval == false then
– decrease speed
else
– increase speed

end)

game.Workspace.Player.Humanoid.Running:Connect(function(speed)
if speed > 0 then
IsRunning.Value = true
else
Isrunning.Value = false
end
end)

2 Likes

This should be something to keep in mind, but thank you for the feedback!

Please remember to use the script formatting tool.
32%20PM

2 Likes

Hello, did you figure out deceleration ? Thanks

1 Like

I mean you could use UIS and detect if the player lets go and gradually stop them.

This isn’t exactly the functionality you described but I think it is what you want. The script is placed in StarterCharacterScripts. If the player is moving and hold shifting then the speed will increase, it will decrease if the player isn’t holding shift. If the player isn’t moving the Walkspeed is set to StartSpeed.

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local character = script.Parent
local Human = character:WaitForChild("Humanoid")

local StartSpeed = 16
local GoalSpeed = 26

local function Run(step)
	local ShiftIsDown = UserInputService:IsKeyDown(Enum.KeyCode.LeftShift)
	local IsStill = Human.MoveDirection == Vector3.new(0,0,0)
	local CurrentSpeed = Human.WalkSpeed
	local Change = 5*step --the lower the multiplicand, the longer it will take to increase and decrease speed
	
	if IsStill then
		Human.WalkSpeed = StartSpeed
	elseif ShiftIsDown and (CurrentSpeed < GoalSpeed) then
		Human.WalkSpeed = Human.WalkSpeed + Change
	elseif (ShiftIsDown == false) and (CurrentSpeed > StartSpeed) then
		Human.WalkSpeed = Human.WalkSpeed - Change
	end
	
	--print("Speed is: " .. CurrentSpeed .. " Moving is: " .. tostring(IsMoving))
end

RunService.Heartbeat:Connect(Run)
2 Likes

Thanks I will check that out.

Like for normal PC, W key, for moving forward, at a normal speed, I guess I am thinking like to make the play skid to a stop, instead of just stopping… maybe changing it from LeftShift to W, and then doing the same for the A,S,D…
The for bonus , having it work on other devices, like console…
Then doing the same for acceleration… might be a cool effect to have in a game…
I’ll play around with the code. thanks again!

I think you want the player’s character to slow down into a idle state from a moving state. If you want to use the Humanoid object, Humanoid.WalkToPoint is the only way I can think of achieving this effect.

Check if the player has stopped moving
Set the WalkToPoint ahead of the direction the player was moving in
Decrease the WalkSpeed until WalkToPoint met.

Getting the point is easier if you are using the default humanoid setup
local Point5StudsAheadPlr = HumanoidRootPart.Position+(Humanoid.MoveDirection*Vector3.new(5,0,5))

1 Like

Cool thanks! I will play around with that too and see if I can get it working.