Some problem with if

I am working on a rpg game and it goes betther then expected :smile:
but I noticed a critical problem whit the AI when I did some testing: The enemy uses two attacks when it is using a spesific move. I would guess I have written something wrong in the function that picks the attack, but I can’t find it…

Note: I’m not including the entire script, but the only part missing is some vars and other things that’s not important to the problem

Note2: written in javascript

//Varibles used for random numbers

var ChanceForCrit;

var AttackPicker;

function UpdateRandom () {

//Set a random number between 1 and 100 giving 1/100 for each number 

ChanceForCrit = Random.Range(1, 100);

AttackPicker = Random.Range(1, 100);

Debug.Log("AttackPicker = " + AttackPicker + " ChanceForCrit = " + ChanceForCrit);

}



function EnemyAttack () {

UpdateRandom();

if (AttackPicker < 20) {
//problem right here: it uses rage AND bite when it's using this attack
	AtkRage();

	}

if (AttackPicker > 20){

	AtkBite();

	}

}



//List of attacks

//----------------------------------------------

function AtkBite () {



var AttackPower = MouseAtk * 10 / 50;



UpdateRandom();



//Check for crits

if (ChanceForCrit < 10) {

	//It's a crit, double the damage

	AttackPower = AttackPower * 2;

	

	//Inform the player

	BattleGUI.BattleInformation += "\nMouse used bite\nIt's a critical hit! Did " + AttackPower + " damage!";

	}

	

	else {

	//It's not a crit :'(, don't do anything

	

	//Inform the player

	BattleGUI.BattleInformation += "\nMouse used bite\nDid " + AttackPower + " damage!";

	}



Arnold.ArnoldHp -= AttackPower;

}



function AtkRage () {



var AttackPower = MouseAtk * 20 / 50;



UpdateRandom();



//Check for crits

if (ChanceForCrit < 10) {

	//It's a crit, double the damage

	AttackPower = AttackPower * 2;

	

	//Inform the player

	BattleGUI.BattleInformation += "\nMouse used rage\nIt's a critical hit! Did " + AttackPower + " damage!";

	}

	

	else {

	//It's not a crit :'(, don't do anything

	

	//Inform the player

	BattleGUI.BattleInformation += "\nMouse used rage\nDid " + AttackPower + " damage!";

	}



Arnold.ArnoldHp -= AttackPower;

}

thanks in advance :smile:

when you call AtkRage, its recalculating your AttackPicker and when it returns to the function after completing AtkRage, AttackPicker is now a different number that might be above 20 (because AtkRage calls UpdateRandom which updates both your randoms!). Im new so I forget how to break out of a function (i think its ‘return’ right?), but you will need to end EnemyAttack after AtkRage() is called, or just add an else since the original number must be above 20

function EnemyAttack () {
	UpdateRandom();
	if (AttackPicker <= 20) {
		AtkRage();
	} else {
		AtkBite();
	}
}

thanks a lot :smile: it helped :smile: