While spawned entity alive loop not working correct

Discussion in 'Skript' started by XP20, Jan 14, 2020.

  1. Hi, I tried to make a boss but this is what came out. The boss is there and it sends: "works in loop1" 3 times before stopping. Heres the code:
    Code (Text):
    command /event [<text>] [<text>]:
        permission: event
        trigger:
            if arg-1 is set:
                if arg-2 is set:
                    if arg-1 is "start":
                        if arg-2 is "pig":
                            summon 1 of pig at location of player
                            set the display name of the spawned pig to "&c&lBoss"
                            set the spawned pig's max health to 150
                            set the spawned pig's health to 150
                            while spawned pig is alive:
                                set {_nearest} to first element of all players
                                loop all players:
                                    send "works in loop1" to loop-player
                                    if distance between loop-player and location of spawned pig is less than distance between spawned pig and {_nearest}:
                                        set {_nearest} to loop-player
                                    make spawned pig pathfind to location of {_nearest} at speed 1.5
                                set {health} to health of spawned pig
                                if {health} is higher than or equal to 20:
                                    if {health} is higher than or equal to 100:
                                        loop all players in radius 5 around spawned pig:
                                            make spawned pig damage loop-player by 2
                                            send action bar "&c&lBoss &7&l>> &cHealth: %{health}%&7/&c150" to loop-player
                                            wait 10 ticks
                                            chance of 10%:
                                                spawn 1 of zombie at location of spawned pig
                                    else:
                                        loop all players in radius 5 around spawned pig:
                                            make spawned pig damage loop-player by 3
                                            send action bar "&c&lBoss &7&l>> &cHealth: %{health}%&7/&c150" to loop-player
                                            wait 10 ticks
                                            chance of 20%:
                                                spawn 1 of zombie at location of spawned pig
                                else:
                                    loop all players in radius 5 around spawned pig:
                                        make spawned pig damage loop-player by 5
                                        send action bar "&c&lBoss &7&l>> &cHealth: %{health}%&7/&c150" to loop-player
                                        wait 10 ticks
                                        chance of 40%:
                                            spawn 1 of zombie at location of spawned pig
                                wait a second
     
  2. so what's wrong? you never explained

    some code improvement recommendations:
    - move pathfind outside of loop all players
    - set variable to a spawned pig instead of using spawned pig directly
     
  3. looking at this post again, I now see your problem, you are looping all players and comparing their location to spawned pig's, but you don't think that the players could be in different worlds than the pig.
     
  4. ^ govindas got your back.

    further, i'd recommend getting basic stuff to work within barren test scripts. if youre unsure about how to do something, do not try to make it work in a complex environment - you'll only get yourself lost.

    further, further: im not usually one to say "you could replace that by getting plugin X", but in this case: MythicMobs will make your life tons easier here. take a look at it, it's especially made to let server owners design their own bosses, ranging from small minions to epic whatevers.