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
  • Help with combat script


    bap

    Recommended Posts

    Hi,

    I would like some help on how I would be able to get a list of NPCs closest to my player, and see if it has the option to attack them, and if it does, to attack them.

    This is the code I have so far.

        @Override
        public boolean accept() {
            return !Players.localPlayer().isHealthBarVisible() && !Players.localPlayer().isAnimating();
        }
    
        @Override
        public int execute() {
            List<NPC> npcs = NPCs.all();
            if (npcs.isEmpty()) {
                return Calculations.random(600,1000);
                //wait for an npc to be in our range
            }
    
            if (npcs.interact("Attack")) {
                sleepUntil(this::isAttacking, 5000);
            }
            return Calculations.random(600,1250);
        }
        private boolean isAttacking() {
            //returns if we are in an attacking animation
            return getLocalPlayer().isAnimating();
        }
    }

    Are there any things that I could add? npcs.interact("Attack") is giving me an error, saying that you can't use it on a list, which is the main problem I'm having right now.

    Link to comment
    Share on other sites

    NPCs is a LIST of NPC's and not an npc object.  When you call .interact() on the list it has no clue what you are doing.  You need to get an NPC out of the list then call .interact() on the npc not the NPCs list 

    Link to comment
    Share on other sites

            NPC attackable = NPCs.closest(n -> n.distance(Players.localPlayer() < maxDistanceHere) && n.hasAction("Attack");

    Something like this ought to suffice for what you said. Change the 'maxDistanceHere' to the maximum distance you'd like it to register npcs at. Additionally you may need to add a query checking if the npc(s) are reachable depending on your use case.

    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.