Auto-translated
function replacecreature()
while (1) do
sleep(2);
local herocr = GetHeroCreatures( "Sarge", CREATURE_ARCHER );
if herocr >= 1 and
GetTownBuildingLevel("cheast_town", TOWN_BUILDING_DWELLING_2 ) == 2 then
RemoveHeroCreatures( "Sarge", CREATURE_ARCHER, herocr );
AddHeroCreatures( "Sarge", CREATURE_LONGBOWMAN, herocr );
sleep(1);
break
end;
end;
end;
startThread(replacecreature);
```
I would write it this way. Unless it's necessary to have only 1 longbowman, then replace herocr in the AddHeroCreatures line with 1.