1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Level up system from kills

Discussion in 'Development' started by JoshXX, Feb 23, 2017.

  1. JoshXX

    JoshXX Spider

    Messages:
    14
    Can someone please help me out. I need help with a level up system like when you get 5 kills you level up to level 2 and to level up to level 3 you need 10 kills.
     
  2. kaliiks

    kaliiks Zombie

    Messages:
    250
    There is lots of ways how to do it lots of peoples using config for that
     
  3. JoshXX

    JoshXX Spider

    Messages:
    14
    Can you help me?
     
  4. kaliiks

    kaliiks Zombie

    Messages:
    250
    Can you share code, if you have some?
     
  5. JoshXX

    JoshXX Spider

    Messages:
    14
    I don't :(
     
  6. kaliiks

    kaliiks Zombie

    Messages:
    250
    Sorry, I can not help you without making any effort to solve your problem.
     
    Palente likes this.
  7. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    This is the plugin development section, if you want someone to code a plugin for you, please use plugin requests
     
    Palente likes this.
  8. kaliiks

    kaliiks Zombie

    Messages:
    250
    If you dont save it to config it remove all after restart use Config
     
  9. JoshXX

    JoshXX Spider

    Messages:
    14
    public function PlayerDeath(PlayerDeathEvent $event){
    $cause = $event->getEntity();
    $name = $cause->getName();
    $this->death[$name] = $this->death[$name]+1; //Add Death
    if($cause instanceof EntityDamageByEntityEvent){
    if($killers instanceof Player){
    $killers = $cause->getDamager();
    $this->kill[$killers->getName] = $this->kill[$killers->getName]+1; //Add Kill
    }
    }
     
  10. kaliiks

    kaliiks Zombie

    Messages:
    250
    Use [PHP ] Code [/PHP ] And you doest not must remove post if is anything bad you can use edit button :D
    and
     
  11. JoshXX

    JoshXX Spider

    Messages:
    14
    PHP:
     public function PlayerDeath(PlayerDeathEvent $event){
                
    $cause $event->getEntity();
                
    $name $cause->getName();
                
    $this->death[$name] = $this->death[$name]+1//Add Death
                
    if($cause instanceof EntityDamageByEntityEvent){
                    if(
    $killers instanceof Player){
                        
    $killers $cause->getDamager();
                        
    $this->kill[$killers->getName] = $this->kill[$killers->getName]+1//Add Kill
                        
    $this->config->save(); 
                    }
                } 
     
  12. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    It looks like $this->death and $this->kills is an array but you have $this->config->save()??? Do you want to save kills and death to a config?
     
  13. JoshXX

    JoshXX Spider

    Messages:
    14
    Oops :eek:

    PHP:
     public function PlayerDeath(PlayerDeathEvent $event){
                
    $cause $event->getEntity();
                
    $name $cause->getName();
                
    $this->death[$name] = $this->death[$name]+1//Add Death
                
    $this->deaths->save();
                    }
                if(
    $cause instanceof EntityDamageByEntityEvent){
                    if(
    $killers instanceof Player){
                        
    $killers $cause->getDamager();
                        
    $this->kill[$killers->getName] = $this->kill[$killers->getName]+1//Add Kill
                        
    $this->kills->save();
                    }
                } 
     
    Last edited: Feb 23, 2017
  14. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You have quite a few issues here. I'm going to dissect your code and point out the issues so you can learn what is wrong and how to prevent it in the future.

    Let's start with this:
    PHP:
      $cause $event->getEntity();
    //Later in your code...
        
    if($cause instanceof EntityDamageByEntityEvent){ 
    This causes issues because you got the player that died, and here you are checking if that Player is an EntityDamageByEntityEvent. Obviously, this doesn't work because the Player is not an Event. You need to get the last damage given to the player before they died. You can do that with this:
    PHP:
     $cause $deathevent->getPlayer()->getLastDamageCause();
     if(
    $cause instanceof EntityDamageByEntityEvent) {
     
    $killer $cause->getDamager();
        } 
    You also used this:
    PHP:
       if($killers instanceof Player){
         
    $killers $cause->getDamager(); 
    The issue here is that you are checking if $killers is an instance of Player, but you don't define what $killers is before you do this check. Before using a variable, ALWAYS define it in some way.

    One thing you did was use +1 to add one to the current value of the kills or deaths for a player. While this is not invalid and won't likely cause errors, you can use $value++ and it will automatically add one to that value. This is debatably simpler.

    Unless you are storing in a file, $this->kills->save() is just wrong. You can't "save" an array. It saves automatically, and stays until reset or overwritten. I assume you want to save the kills and deaths of each player in a file, right? In that case, check out this post by SOFe that explains managing data.

    If you are still confused, or you just don't want to bother with this, I would actually enjoy writing a small plugin like this, if needed.
     
    Skullex likes this.
  15. JoshXX

    JoshXX Spider

    Messages:
    14
    I'm new to this and I'm a bit confused. I would really appreciate it if you could write it for me.
     
  16. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Okay, no problem. Just keep at it and you will catch on, I promise. :)
     
    JoshXX likes this.
  17. JoshXX

    JoshXX Spider

    Messages:
    14
    Thanks a lot :D
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.