Skip to content

Scripts for beginners.

User Avatar
#242
Auto-translated
Syryus

Hello!

As a result, I got the following script:

preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
for key, name in preserve_heroes do
if name == heroName then
local PreserveHero=1
end;
end;
if not PreserveHero then
         StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
     end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );

It intercepts all heroes without exception...
I even tried having Ilfina ("Itil") enter the region - a battle starts, and the console shows the message: Value was NIL when getting global with name 'PreserveHero'
Your error is related to the scope of the variable. If you declare a local variable inside a certain block, it will only be known within that block. In other words, in your example, the PreserveHero variable is only known in the block
if name == heroName then
local PreserveHero=1
end;

For the rest of the function, the variable is not defined, which causes the error. You can use a global variable, as suggested above, but this is generally bad practice, so you should simply change the scope of the local variable.

preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
local PreserveHero
for key, name in preserve_heroes do
if name == heroName then
PreserveHero=1
end;
end;
if not PreserveHero then
StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );


or, even simpler, use the built-in contains() function

preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
if not contains(preserve_heroes, heroName) then
StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );

 

Нет войне.