(Godot4.2.1) Acceleration code makes the player's velocity sometimes go to infinity

Godot Version
Godot 4.2.1

I am attempting to add acceleration to a player, but something is messing up, and the player teleports offscreen and the printed x velocity is inf. Any help would be appreciated, the code is below:

extends CharacterBody2D

@export var MaxSpeed = 300.0
@export var Accelerate=false
@export var Acceleration=10.0
@export var Deceleration=10.0
@export var MaxJumpVelocity = -500.0
@export var MaxAirJumpVelocity=-200.0
@export var Flying=false
@export var MaxFlyingTime=2
@export var Gliding=false
@export var MaxGlidingTime=60
@export var WallSliding=false
@export var Dashing=false
@export var MaxDashSpeedIncrease=120.0
@export var MaxDashTime=1
@export var MaxAirDash=1
@export var MaxDashCooldown=180
@export var DashImmunity=false
@export var MaxKnockbackResistance=5
@export var MaxAirJumps=0
var Direction=0
var Speed=0
var FlyingTime=0
var GlidingTime=0
var DashSpeedIncrease=0
var DashTime=0
var JumpVelocity=0
var AirDash=0
var DashCooldown=0
var KnockbackResistance=0
var IsTouchingWall=false
var AirJumps=0
var Accelerating=false
var Decelerating=false
var AirJumpVelocity
var PrevVelocity=Vector2(0,0)

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

func _ready():

if not Accelerate:
	Speed=MaxSpeed
FlyingTime=MaxFlyingTime
GlidingTime=MaxGlidingTime
DashSpeedIncrease=MaxDashSpeedIncrease
DashTime=MaxDashTime
JumpVelocity=MaxJumpVelocity
AirDash=MaxAirDash
DashCooldown=MaxDashCooldown
KnockbackResistance=MaxKnockbackResistance
AirJumps=MaxAirJumps
AirJumpVelocity=MaxAirJumpVelocity

func _physics_process(delta):
# Add the gravity.
print(velocity)
print(PrevVelocity)
if not is_on_floor():
velocity.y += gravity * delta

if is_on_floor():
	AirJumps=MaxAirJumps
	AirDash=MaxAirDash

# Handle jump.
if Input.is_action_just_pressed("Jump") and is_on_floor():
	velocity.y = JumpVelocity
elif Input.is_action_just_pressed("Jump") and not is_on_floor():
	if AirJumps>0:
		velocity.y = AirJumpVelocity
		AirJumps-=1

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("Left", "Right")
if not Accelerate:
	if direction:
		velocity.x = direction * Speed
	else:
		velocity.x = move_toward(velocity.x, 0, Speed)
else:
	if direction==0:
		if Speed>0:
			Decelerating=true
		if Decelerating:
			Speed-=Deceleration*delta
		if Speed<=0:
			Decelerating=false
			Speed=0
		velocity.x = PrevVelocity.x * Speed
	elif direction>0:
		if PrevVelocity.x>=0:
			if Speed<MaxSpeed:
				Accelerating=true
			if Accelerating:
				Speed+=Acceleration*delta
			if Speed>=MaxSpeed:
				Accelerating=false
				Speed=MaxSpeed
			velocity.x = direction * Speed
		elif PrevVelocity.x<0:
			if Speed>0:
				Decelerating=true
			if Decelerating:
				Speed-=Deceleration*delta
			if Speed<=0:
				Decelerating=false
				Speed=0
			if Speed==0:
				velocity.x = direction * Speed
	elif direction<0:
		if PrevVelocity.x<=0:
			if Speed<MaxSpeed:
				Accelerating=true
			if Accelerating:
				Speed+=Acceleration*delta
			if Speed>=MaxSpeed:
				Accelerating=false
				Speed=MaxSpeed
			velocity.x = direction * Speed
		elif PrevVelocity.x>0:
			if Speed>0:
				Decelerating=true
			if Decelerating:
				Speed-=Deceleration*delta
			if Speed<=0:
				Decelerating=false
				Speed=0
			if Speed==0:
				velocity.x = direction * Speed
print(Speed)
PrevVelocity=velocity

move_and_slide()

You don’t divide anywhere so it’s hard to say.

I would setup an assert or if/breakpoint when ever the velocity goes out of range. then inspect your class values to see if you can spot any outliers.

If abs(velocity.x) > 10000:
  breakpoint