Why is my character not always jumping when I press the jump button?

I have my game coded where my character jumps using the space bar. Sometimes when I press it he does not always jump. It seems to happen more when I jump consecutively.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    public float moveSpeed;
    public float jumpForce;
    public CharacterController controller;
    private Vector3 moveDirection;
    public float gravityScale;



    // Use this for initialization
    void Start()
    {

        controller = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {

        moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y, 0);


        if (controller.isGrounded)
        {
            moveDirection.y = 0f;
            if (Input.GetButtonDown("Jump"))
            {
                moveDirection.y = jumpForce;
            }
        }
        moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
        controller.Move(moveDirection * Time.deltaTime);


        if (!Mathf.Approximately(moveDirection.x, 0))
        {
            Vector3 direction = moveDirection;
            direction.y = 0;
            transform.rotation = Quaternion.LookRotation(direction.normalized);
        }
        

    }
}

Good day.

For the code you post…:

if (controller.isGrounded)
         {
             moveDirection.y = 0f;
             if (Input.GetButtonDown("Jump"))
             {
                 moveDirection.y = jumpForce;
             }
         }

There can be only 2 things:

1- controller.isGrounded is still false.

2- GetButtonDown detects when the buton is pressed (dont detect hold the button). If it was pressed at the start of the frame, it will not detect it. You can change the detection to:

  if (Input.GetButtonDown("Jump") || Input.GetButton("Jump"))
              {
                  moveDirection.y = jumpForce;
              }

So now detects the start of pressing and the hold.

Bye!