Problems with "If" commands?

When trying to make a workshop gamemode, I’ve been running into an issue using “If” commands to set specific abilities and skills to a player, depending on what hero they are currently playing. Strangely enough, I’ve found that I sometimes have success with “If” commands, but don’t other times.

When trying to create different HUD Text for a player, dependent on what hero they’re playing, I used a conditional command in Actions as follows:
If(Compare(Hero of(Event Player), ==, Hero(Soldier: 76)))
Yet, for some reason, the game won’t generate the HUD.

However, when creating a unique ability for Soldier: 76, I used:
If(Compare(Hero(Soldier: 76), ==, (Hero of(Event Player)))
Oddly enough, the ability works as expect.

For extra clarity, both of these are set under Rules which are “Ongoing - Each Player, Team All, Player All.” I know the two strings I typed are in different order, but I’ve tried the first in various ways to no avail. Is there some kind of syntax issue here that I’m not aware of or is the “If” command just broken?

If this is your whole code, then yes, you made a syntax error. You forgot the “end” action.

The “if” action is used this way:

  • if(condition)
  • do actions
  • end()

Or this way:

  • if(condition)
  • do actions if condition == true
  • else()
  • do actions if condition == false
  • end()

Or this way:

  • if(condition1)
  • do actions if condition1 == true
  • elseif(condition2)
  • do actions if condition1 == false and condition2 == true
  • end()

If the missing “end” is not the problem in your code, you may post your whole code here.

1 Like

Still doesn’t work whether I used “end” or not. Strangest part is that the ability I script worked with or without the “end” function.

Here’s what the script I’m trying to make for the HUD looks like.

variables
{
	player:
		1: A1
		2: A2
		3: A3
		7: HEAT
		17: RAGE
		18: HUD0
		19: HUD1
		20: HUD2
		21: HUD3
		22: HUD4
		29: A4
}

rule("HUD Text")
{
	event
	{
		Ongoing - Each Player;
		All;
		All;
	}

	actions
	{
		If(Is Alive(Event Player));
			If(Compare(Hero Of(Event Player), ==, Hero(Soldier: 76)));
				Create HUD Text(Event Player, Custom String("RAGE {0} {1}/{2}", Hero Icon String(Hero(Soldier: 76)), Player Variable(Event Player,
					RAGE), 100), Null, Null, Top, 1, Orange, White, White, Visible To Sort Order and String, Default Visibility);
				Set Player Variable(Event Player, HUD0, Last Text ID);
				Create HUD Text(Event Player, Custom String("Shade Punch [Melee] {0} {1}", Icon String(Dizzy), Player Variable(Event Player, A3),
					Null), Null, Null, Left, 3, White, White, White, Visible To and String, Default Visibility);
				Set Player Variable(Event Player, HUD3, Last Text ID);
				Create HUD Text(Event Player, Custom String("Shadow Step [Crouch + Sprint] {0} {1}", Icon String(Arrow: Left), Player Variable(
					Event Player, A4), Null), Null, Null, Left, 4, White, White, White, Visible To and String, Default Visibility);
				Set Player Variable(Event Player, HUD4, Last Text ID);
			Else If(Compare(Hero Of(Event Player), ==, Hero(Genji)));
				Create HUD Text(Event Player, Custom String("HEAT {0} {1} / 9 Bars", Hero Icon String(Hero(Genji)), Player Variable(Event Player,
					HEAT), Null), Null, Null, Top, 0, Lime Green, White, White, Visible To and String, Default Visibility);
				Set Player Variable(Event Player, HUD0, Last Text ID);
				Create HUD Text(Event Player, Custom String("Retaliate [Ability 1] {0} {1}", Icon String(Exclamation Mark), Player Variable(
					Event Player, A1), Null), Null, Null, Left, 1, White, White, White, Visible To and String, Default Visibility);
				Set Player Variable(Event Player, HUD1, Last Text ID);
				Create HUD Text(Event Player, Custom String("Swift Strike [Ability 2] {0} {1}", Icon String(Arrow: Left), Player Variable(
					Event Player, A2), Null), Null, Null, Left, 2, White, White, White, Visible To and String, Default Visibility);
				Set Player Variable(Event Player, HUD2, Last Text ID);
				Create HUD Text(Event Player, Custom String("Steamburst [Melee] {0} {1}", Icon String(Eye), Player Variable(Event Player, A3),
					Null), Null, Null, Left, 3, White, White, White, Visible To and String, Default Visibility);
				Set Player Variable(Event Player, HUD3, Last Text ID);
				Create HUD Text(Event Player, Custom String("Cool Down [Crouch] {0} ", Icon String(Arrow: Down), Null, Null), Null, Null, Left, 4,
					White, White, White, Visible To and String, Default Visibility);
				Set Player Variable(Event Player, HUD4, Last Text ID);
			End;
		End;
	}
}

Ok, no wonder it doesnt work. You have to put the “is alive(event player)” into the condition part of the rule.
Otherwise your code gets executed as soon as the player joined the match, when he obviously neither has spawned yet nor “is alive”.
So the fixed code would look similar to this:

ongoing each player - all - all

condition:
- is alive(event player) == true

action:
- if(compare(hero of(event player), ==, hero(soldier76)))
- *do stuff*
- else if(compare(hero of(event player), ==, hero(genji)))
- *do stuff*
- end()

This is because a rule triggers when all of its conditions are “true”. After this the rule only triggers again after at least one of its conditions has been “false” and then all of its conditions are “true” again.
As you dont have conditions in your rule, they will never get “false”, so the rule wont execute a second time.

1 Like