Is there a better way to continuously check the distance between an object moving away and the player's character?

I’m making thor’s hammer wherein I’ve to constantly check for the distance between the hammer flying away and my character.
So far I’ve done it like this:

local Distance2 = nil
		while true do
			wait()
			Distance2 = (HumanoidRootPart.Position - Mjolnir.Position).Magnitude
		end
		if Distance2 > 100 then
			Mjolnir.Anchored = true
			Mjolnir.BodyVelocity:Destroy()
		end

But it doesn’t work for some reason, is there a better way?

1 Like

Solution:

local Distance2 = nil
while true do
	  wait()
	  Distance2 = (HumanoidRootPart.Position - Mjolnir.Position).Magnitude
      if Distance2 > 100 then
			Mjolnir.Anchored = true
			Mjolnir.BodyVelocity:Destroy()
	  end 
end

The reason it won’t work is because the last if statement detects whether distance is greater than 100 or not ONLY ONCE, however if you put the if statement into the while loop it will constantly find whether the distance is greater than 100 or not until the loop stops

1 Like