1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

  2. LOOKING FOR A VERSION OF SKRIPT?

    You can always check out our Wiki for downloads and any other information about Skript!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Nearest entity to location

Discussion in 'Skript' started by PatoFrango, Jul 21, 2018.

Thread Status:
Not open for further replies.
  1. PatoFrango

    PatoFrango Active Member

    Joined:
    Jul 12, 2017
    Messages:
    240
    Likes Received:
    14
    Hi,

    I'm sorry if I'm overposting, but how can I detect the nearest entity to a location/entity? Is there like a "nearest" expression? I've done some research and I saw a few people using that, but it didn't work for me. If there isn't, what would be the best way of doing so? I've tried coding it manually but the moment I thought of using multi-dimensional arrays (which to me seems to be the most logical way), I realized that Skript doesn't have such things. At least as far as I know.
     
  2. Best Answer:
    Post #2 by Donut, Jul 21, 2018
  3. Donut

    Donut Well-Known Member

    Joined:
    Mar 27, 2017
    Messages:
    1,340
    Likes Received:
    176
    Medals:
    Code (Skript):
    1. function nearestEntity(loc: location) :: entity :
    2.     set {_nearest} to first element of all entities in world of {_loc}
    3.     loop all entities in world of {_loc}:
    4.         if distance between loop-entity and {_loc} is less than distance between {_loc} and {_nearest}:
    5.             set {_nearest} to loop-entity
    6.     return {_nearest}
     
  4. aeim

    aeim Member

    Joined:
    Feb 21, 2017
    Messages:
    38
    Likes Received:
    5
    Code (Skript):
    1. loop all entities in world of {_player}:
    2.     loop-entity is not {_player}
    3.     set {_dist} to (distance between {_player} and loop-entity *1000) + (random integer between 0 and 999)/1000
    4.     set {_list::%{_dist}%} to loop-entity
    5. broadcast "%the first element of {_list::*}%"
    Skript mechanics :emoji_sunglasses:
     
Thread Status:
Not open for further replies.

Share This Page

Loading...