Is this a good way to do acceleration?

Hello, i am trying to make a fast paced game inspired on games like Sonic The Hedgehog, and acceleration is basically a must, i’ve created a pretty simple acceleration code which gets the job done, however, i am worried this might not be the most efficient take on this, or if its bad practice/laggy,
is there a much more efficient approach on this?

This is the script, it’s placed on Humanoid.Changed Event,
it checks if movedirection.magnitude is bigger than 0, if it is, it does a loop in which the player accelerates by their current “mode” acceleration value, if their current speed is bigger than their mode’s set maxspeed it will slowly decrease it until it reaches a propiate value,
if theyre not moving, they will slowly deccelerate by a set value of 2, this is done via while do loops

Humanoid.Changed:Connect(function(MoveDirection)
if (Humanoid.MoveDirection).magnitude > 0 and not moving and char:GetAttribute("CanAccelerate") == true then
		moving = true
		
		while moving == true do
			wait()
			-- Acceleration
			if char:GetAttribute("Choosing") ~= true then
				local AccelerationValue = char:GetAttribute("Acceleration")
				if ExpectedSpeed.Value > char:GetAttribute("MaxSpeed") then
					Humanoid.WalkSpeed -= .15
					ExpectedSpeed.Value -= .15
					print("too much speed, deccelerating")
				else
					print("accelerating")
					Humanoid.WalkSpeed = math.clamp(Humanoid.WalkSpeed + AccelerationValue, 16, char:GetAttribute("MaxSpeed"))
					ExpectedSpeed.Value = math.clamp(ExpectedSpeed.Value + AccelerationValue, 16, char:GetAttribute("MaxSpeed"))
				end
			end
-- If not moving, deccelerate by 2
	elseif (Humanoid.MoveDirection).magnitude == 0 and moving and char:GetAttribute("CanAccelerate") == true then
		moving = false
	while moving == false do
			task.wait()
			Humanoid.WalkSpeed -= 1
			ExpectedSpeed.Value -= 1
		end
		print("stopped")
		
	end

This is each modes values btw

local Properties = {
	[1] = {
		Acceleration = 0.15,
		MaxSpeed = 30 
	},
	[2] = {
		Acceleration = .5,
		MaxSpeed = 60
	},
	[3] = {
		Acceleration = 0.3,
		MaxSpeed = 37
	}
}

feedback is appreciated

local Conversion = 196.2 / 9.8 -- convert from studs to meters
local Acceleration = Vector3.new(0, 1 - 9.8, 0) * Conversion -- 1 is the gravity multiplier

I used this in a projectile module not sure if it would help in walkspeed but you can try

1 Like

not quite sure how id implement this though

you could try lerping the walkspeed by the acceleration amount from the properties table