--// Functions

function Forward()
	Storage.Sound:FireServer(runSound, true)
	Storage.Sound:FireServer(idleSound, false)
	
	while true do
		rst:Wait()
		if ForwardKeyDown then
			if speed < maxSpeed then
				speed = speed + 0.5
			elseif speed > maxSpeed then
				speed = maxSpeed
			end
			
			local lv = seat.CFrame.lookVector
		
			local maxVelo = ((2*lv)*maxSpeed)
			local newVelo = ((2*lv)*speed)

			Storage.UpdateBV:FireServer(newVelo, BV)
		end
		
		if not ForwardKeyDown and not BackwardKeyDown then
			speed = speed - 1
			
			local lv = seat.CFrame.lookVector
			
			local newVelo = ((2*lv)*speed)
			
			Storage.UpdateBV:FireServer(newVelo, BV)
			
			if speed <= 0 then
				speed = 0
				
				local newVelo = ((2*lv)*0)
				Storage.UpdateBV:FireServer(newVelo, BV)
				
				Storage.Sound:FireServer(idleSound, true)
				Storage.Sound:FireServer(runSound, false)
				break
			end
			
			if BackwardKeyDown and not ForwardKeyDown then
				break
			end
		end
	end
end

function Backwards()	
	Storage.Sound:FireServer(runSound, true)
	Storage.Sound:FireServer(idleSound, false)
	
	while true do
		rst:Wait()
		if BackwardKeyDown then
			if speed > -maxSpeed then
				speed = speed - 0.5
			elseif speed < -maxSpeed then
				speed = -maxSpeed
			end
			
			local lv = seat.CFrame.lookVector
			local newVelo = ((2*lv)*speed)
			
			Storage.UpdateBV:FireServer(newVelo, BV)
		end
		
		if not BackwardKeyDown and not ForwardKeyDown then
			speed = speed + 1
			
			local lv = seat.CFrame.lookVector

			local newVelo = ((2*lv)*speed)

			Storage.UpdateBV:FireServer(newVelo, BV)
			
			
			if speed <= 0 then
				speed = 0
				
				local newVelo = ((2*lv)*0)
				Storage.UpdateBV:FireServer(newVelo, BV)
				
				Storage.Sound:FireServer(idleSound, true)
				Storage.Sound:FireServer(runSound, false)
				break
			end
		end
		
		if not BackwardKeyDown and ForwardKeyDown then
			break
		end
	end
end

function turnRight()
	while RightTurnKeyDown do
		wait()
		
		local newAV
		
		if ForwardKeyDown or BackwardKeyDown then
			newAV = Vector3.new(0,-0.5,0)
		else
			newAV = Vector3.new(0,-0.4,0)
		end
		
		Storage.UpdateAV:FireServer(newAV, BAV)
		
		if not RightTurnKeyDown then
			local newAV = Vector3.new(0,0,0)
			Storage.UpdateAV:FireServer(newAV, BAV)
		end
	end
end

function turnLeft()
	while LeftTurnKeyDown do
		wait()
		
		local newAV
		
		if ForwardKeyDown or BackwardKeyDown then
			newAV = Vector3.new(0,0.5,0)
		else
			newAV = Vector3.new(0,0.4,0)
		end
		
		Storage.UpdateAV:FireServer(newAV, BAV)
		
		if not LeftTurnKeyDown then
			local newAV = Vector3.new(0,0,0)
			Storage.UpdateAV:FireServer(newAV, BAV)
		end
	end
end

function EndControl(bool)
	Storage.Sound:FireServer(runSound, false)
	Storage.Sound:FireServer(idleSound, false)
	
	Equipped = false
	Storage.Stop:FireServer(BV, BAV)
	cam.CameraType = Enum.CameraType.Custom
	
	script:Destroy()
end

function flipTank()
	Storage.Flip:FireServer(seat, BG)	
end
