Solved Set random player velocity

Discussion in 'Spigot Plugin Development' started by Kompye, Sep 15, 2019.

  1. Hello. I'm trying to make a plugin that launches the player in random directions, but I can't get setVelocity working. Here is what I've tried so far:
    Code (Java):
                Player target = Bukkit.getPlayerExact(args[0]);
                int max = 7;
                int min = -7;
                int count = 0;
                int range = max - min + 1;
                Vector velocitate;
                for(int i = -7; i <= 7; i++) {
                    count++;
                    int rand = (int) (Math.random() * range) + min;
                    if(count == 1) {
                    velocitate.setX(rand);
                    }
                    if(count == 2) {
                        velocitate.setY(rand);
                    }
                    if(count == 3) {
                        velocitate.setZ(rand);
                    }
                    if(count == 4) {
                    break;
                    }
                }
                target.setVelocity(velocitate);
                return true;
     
  2. You never initialised 'velocitate', it's null.
    You should learn to read stacktraces, that way you could have figured that out on your own.
     
  3. Worked, thanks!