I just saw a Dev King tutorial on magnitude, can someone please explain line 14 (Where he sets the agro distance equal to the magnitude) to me please?
That line is essentially redefining how far the zombie should look in order to change its target. Basically, if the next target it finds is closer than the last target it found, it will chase that target instead. The line local aggroDistance = 100
defines the initial search parameter.
Oh yea! Thank you so much! So basically it would check if the player it found is indeed the closest player or not, correct?
Yes; however, the script will find anything in the Workspace with a Humanoid
and Torso
item in it, which could be NPC’s, as well. Moreover, it will not chase R15 models, because those do not have a Torso
but rather an UpperTorso
and LowerTorso
.
Also, wouldn’t it make more sense for it to be magnitude <= agroDistance ?
Building off @MysteriousVagabond 's comment:
To make this more performant you can create an array that contains the players and search through it instead so you don’t have to iterate through every child of workspace
.
Basically it’s how far the closest spider is. And if it finds a spider closer than that spider, it will start attacking the closer spider.
It could, but it would have an (albeit very minimal) impact on behavior. If two characters were equidistant from each other, the zombie would chase the second player it found over the first.
In functionality, this difference would be undiscernible since both players would be the same distance from the zombie. By making the comparison less-than rather than less-than-or-equal-to, you are saving a small amount of performance because the script will not need to run for anything that is athe same distance away as what it has already found.
My question is, let’s say the human it found’s magnitude was 25, so now the agro distance would be set to 25, however when it runs the script again it doesn’t find a human closer than that one, so it will go with the same one, but the agro distance is the SAME as the magnitude i.e 25. However it says agro distance > magnitude, in this example it’s the same?
Also, I really appreciate the help!
When the code runs to find a target, it is only performing one search during that test, just testing each player. When the code finds a player and changes aggroDistance
, it is only making that change for that singular test because aggroDistance
is scoped to the findTarget()
function.
If the code finds a player that meets the condition, it returns that value. One second later, the code performs another search; however, when this search starts, aggroDistance
is reset to 100 because the code starts over from the beginning of findTarget()
, within with aggroDistance
is declared.
What’s the point, if it’s going to reset?
Think of it like a sort of “lazy polling” system.
It is consistently polling every player (or every model with a humanoid in workspace) and determining which object is closest. After the first run through of this script, the agro distance is set to the nearest player - though this agro distance holds no real significance outside of determining which player to attack.
After another run through the acquired data is reset to 100 and it once more runs through the loop to determine the nearest player (nullifying the “25 > 25” example). It resets to uphold accurate data regarding who the closest player is - if it kept data from previous iterations you would easily be able to break the zombie by standing right next to it, waiting for it to walk towards you and then running away from it.
If the code did not reset, then eventually the zombie would reach a point where its aggroDistance
would be so short that it would never find another player to chase and would simply stop moving. By resetting the distance to 100, it ensures that the zombie is always searching for any players within its range. It will still only chase the closest one.
Yea, but then what really is the point of setting the agro distance to the zombie it finds, if it’s just going to reset?
The change of the distance allows the zombie to determine if the next player it finds is closer than the last player it found, but only for that one search.
Each search only moves the zombie to one location. The “chasing” that the zombie demonstrates is the collection of searches/moves it executes over time. If the aggroDistance
did not reset each search, then the zombie would only chase a player if they were closer to the zombie than the closest player it ever found. If that player disconnected, died, or even moved farther away than it was the first time the zombie moved towards it, the zombie would stop moving and just stand still. The initial declaration provides a base, maximum distance from within which to search, even if players have moved farther away.
Yes, but why is it defined at the very end? It just sets the aggroDistance to the magnitude and then ends the script and resets. If it were to be effective it would run the if statement.
The number only resets when the code runs from the top–that is, when findTarget()
is called. When the number is changed farther down in the code, that code is inside a for loop
.
aggroDistance
will stay as the shorter distance each time the code runs through the for loop
because aggroDistance
’s scope is greater than the scope of the for loop
.
If you would like to learn more about scopes, check out here: Scope | Documentation - Roblox Creator Hub
Oh yea! I didn’t even notice the for loop! It all makes so much sense now! Thank you so much for your help!
Thank you for your help! Much appreciated!