How to check if Magnitude is < x

I have an NPC that is supposed to move to certain spot before a script move the NPC again. I am using PathfindingService for the movement.

I am trying to use Magnitude to check if the NPC within a certain range of a Vector3 Value before being moved again.

This is the current script:

Summary
local Move

local Root = workspace.NPCs.Feugazi.HumanoidRootPart
local Humanoid = Root.Parent.Humanoid
local Move1 = workspace.Move1
local Magnitude = (Root.Position-Move1.Position).Magnitude

print("Manitude should print")
print(Magnitude)


-- CHECKS NPC LOCATION
if Magnitude ~= 2 then
	
	print("Waiting for less than 2 magnitude")
	
	repeat wait() until Magnitude < 1.5 
	
		print("Magnitude Worked")
		print(Root.Position)
		
		Move = require(script:WaitForChild("Move1"))
		
	
end

The script is working up until repeat wait() until Magnitude < 1.5 , and is not executing the two prints after that.

Is there a better way for me to check if the magnitude is within a certain range? Am I not noticing an error within my script?

EDIT:
I used the Command Bar to check the Magnitude while testing, and it is within the 1.5 range that I have set, but the script is still not moving past repeat wait()

1 Like

magnitude will be always the same since you are not setting the variable again, refresh variable

3 Likes

Why do you check if Magnitude is not equal to 2? You also never update Magnitude so the code does pretty much the same as

local e = 0
repeat wait() until e == 5 end
1 Like

You are only setting magnitude 1 time as stated above to fix this you need to get it each time the repeat loop runs through to check the new magnitude of the 2 parts here is where it should be in the below script:

local Move

local Root = workspace.NPCs.Feugazi.HumanoidRootPart
local Humanoid = Root.Parent.Humanoid
local Move1 = workspace.Move1
local Magnitude = (Root.Position-Move1.Position).Magnitude

print("Manitude should print")
print(Magnitude)


-- CHECKS NPC LOCATION
if Magnitude ~= 2 then

	print("Waiting for less than 2 magnitude")

	repeat wait() 
		Magnitude = (Root.Position-Move1.Position).Magnitude
	until Magnitude < 1.5 

	print("Magnitude Worked")
	print(Root.Position)

	Move = require(script:WaitForChild("Move1"))


end
1 Like

@Mixu_78 @Nyonic

Yeah thanks lol, I just didnt think about Magnitude being defined earlier in the script and that it was checking a static value like that

2 Likes

Every variable you make off from anything will never change unless you set a new value

1 Like

I have only been scripting for about a week so I still forget about things like that, but thank you for fixing this so fast