Need help understanding magnitude in this script

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

2 Likes