Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Attack closest NPC in an area that's not in combat.


    PapaH

    Recommended Posts

    Hey guys!

    I'm trying to write a script that attacks NPCs in a specific area that are not in combat. My script already checks whether there are enemies in the specific area to attack and wait until out of combat to attack another. How would you go about writing the ! .isInCombat check?

    This is what I was thinking, but it doesn't seem to be working:

    if (attackSpot.contains(NPCs.closest("Sand Crab")) && !NPCs.closest("Sand Crab").isInCombat()){
    //if (NPCs.closest(crab -> crab != null && crab.getName() == "Sand crab" && attackSpot.contains(crab)).exists()){
        log("Attack Sand Crab");
        NPCs.closest("Sand Crab").interact("Attack");

     

    I thought maybe this would work, but it doesn't attack anything.

    NPC SandCrab = NPCs.closest(crab -> crab != null && crab.getName() == "Sand Crab" && !crab.isHealthBarVisible() && attackSpot.contains(crab));
    //if (attackSpot.contains(NPCs.closest("Sand Crab")) && !NPCs.closest("Sand Crab").isInCombat()){
    if(SandCrab != null && !getLocalPlayer().isInCombat()){
        log("Attack Sand Crab");
        SandCrab.interact("Attack");
    Link to comment
    Share on other sites

    Thought I had it with the last edit, but It wouldn't actually attack anything. Still trying to figure out the best .isincombat check while alscho checking if area.contains

    Link to comment
    Share on other sites

    NPC monster = NPCs.closest(crab -> crab != null
    					&& (!crab.isInCombat() || crab.isInteracting(getLocalPlayer())) && crab.getName() == "Sand Crab");
    
    if(!getLocalPlayer().isInCombat())
    	if(monster != null)
    		monster.interact("Attack");

     

    Would something like this work?

    Link to comment
    Share on other sites

    Hello there @PapaH
    You would want to check if the crab is not in combat in the npc predicate , i also noticed that you are using == for a String , That doesnt work in java , you need to use .equals for Strings in java .

    Your predicate should look something like 
    NPC SandCrab = NPCs.closest(crab -> crab != null && crab.getName().equals("Sand Crab") && !crab.isInCombat() && attackSpot.contains(crab));

    Link to comment
    Share on other sites

    3 hours ago, Hosfad said:

    Hello there @PapaH
    You would want to check if the crab is not in combat in the npc predicate , i also noticed that you are using == for a String , That doesnt work in java , you need to use .equals for Strings in java .

    Your predicate should look something like 
    NPC SandCrab = NPCs.closest(crab -> crab != null && crab.getName().equals("Sand Crab") && !crab.isInCombat() && attackSpot.contains(crab));

    That's pretty much exactly what I was looking for, thank you.

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.