musket gun script is not working

this script is supposed to make the gun shoot once and then reload but it just lets me keep shooting i am pretty much a beginner with scripting

here is the script

  var reloadTime = 5;
  var totalAmmo = 10;
  var ammoCount = 1;
                                                                                                                                   
  function Update ()   {                                   
         
     if (ammoCount >= 1) {
       CanShoot = true;
     }
     
     if (ammoCount <= 0) {
     CanShoot = false;
     }
     
     if (CanShoot == false) {
     reload();
     }
     
     if (CanShoot == true) { 
     Shoot();
     }
     }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
  function Shoot () {
  
  var hit : RaycastHit;
  var ray: Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));

  if (Input.GetMouseButtonDown(0))
  {
  if (Physics.Raycast (ray, hit, 100))
  {
  hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
  {ammoCount -= 1;
}
}
}
} 
 
 function reload () {
 
 if (Input.GetKeyDown("r")) {
 
 yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
 
 ammoCount += 1; //adds ammo to our "clip" based off the reloadAmount
 
 totalAmmo -= 1; //subtracts whatever the reloadAmount was from our total ammo every time we reload
 
}                                  
}

It looks to me like you are only reducing the ammo count if your raycast is detecting a hit, whereas it should subtract ammo regardless of a hit.

function Update ()   
{                                   
	if (ammoCount >= 1) 
	{
		CanShoot = true;
	}

	if (ammoCount <= 0) 
	{
		CanShoot = false;
	}

	if (CanShoot == false) 
	{
		reload();
	}

	if (CanShoot == true) 
	{ 
		Shoot();
	}
} 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
function Shoot () 
{

	var hit : RaycastHit;
	var ray: Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));

	if (Input.GetMouseButtonDown(0))
	{
		if (Physics.Raycast (ray, hit, 100))
		{
			hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
		}
		ammoCount -= 1;
	}
} 
 
 function reload () 
 {
	 if (Input.GetKeyDown("r")) 
	 {
		 yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
		 
		 ammoCount += 1; //adds ammo to our "clip" based off the reloadAmount
		 
		 totalAmmo -= 1; //subtracts whatever the reloadAmount was from our total ammo every time we reload
	}                                  
}

Hey There,

You can make your code a lot simpler by removing that can fire checks that you are doing. I have made a quick example of a simple firing script. It is written in C# so you will have to convert it (no copying and pasting :slight_smile: ) it should not take to long.

float currentAmmo = 5.0f;
float currentReloadTime = 0.0f;
bool isReloading = false;

const float RELOAD_TIME = 2.0f;
const int MAX_AMMO = 8; 


void Update()
{
    if (Input.GetKeyDown(KeyCode.R))
    {
        if (currentAmmo != MAX_AMMO && !isReloading) // if we already have max ammo lets leave or if we are already reloading
        {
            isReloading = true; // We are now reloading
            currentReloadTime = RELOAD_TIME; //Set our current load time 
        }
    }

    if (isReloading)
        ReloadStep();
    else
        GunStep();

 
}

void GunStep()
{
    if (currentAmmo <= 0)
        return; //No ammo so we leave.

    if (!Input.GetMouseButtonDown(0)) //the '!' means Not. so the mouse button is Not Down 
        return; //We are not firing so we don't need to be here

    //We fired our gun so we need to use a bullet 
    currentAmmo--;

    //Start our making our raycast
    RaycastHit hit;
    Ray ray = Camera.main.ScreenPointToRay(new Vector3((float)Screen.width * 0.5f, (float)Screen.height * 0.5f, 0.0f));

    //Fire the ray and tell anything we hit to apply damage
    if (Physics.Raycast(ray, out hit, 100))
    {
        hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    }
}

void ReloadStep()
{
    currentReloadTime -= Time.deltaTime; //We count down

    if( currentReloadTime <= 0.0f )
    {
        isReloading = false;
        currentAmmo = MAX_AMMO;
    }
}

As you can see you don’t need much more then that. Let me know if you have any questions.

Regards,

Edited: Added reloading

thank you all very much i sorted it all out