How can I fix this script?

Hello, I’m trying to create a module for my game, so that NPC’s can move to another NPC.

But even though the positions are the same as defined in the property’s of the parts, the script doesn’t ever stop, and it never stops making one NPC follow another.

	while currentPosition ~= designatedPosition do
		if currentPosition == designatedPosition then
			print("returned.")
			return
		end

--\\ Module Made by Star \\--

--[[ --
MoveX:(
Part2 References The Second Part that Part1 is supposed to go to.
)

taskedLocked = Boolean that tells us if their is movement currently going on.

time = time / speed
]] --
local module = {}

function module:MoveTo(Part, Part2, taskLocked, speed)
	local currentPosition = Part.Position
	local designatedPosition = Part2.Position
	
	local cPositionX = Part.Position.X
	local cPositionZ = Part.Position.Z
	
	local dPositionX = designatedPosition.X
	local dPositionZ = designatedPosition.Z
	
	while currentPosition ~= designatedPosition do
		if currentPosition == designatedPosition then
			print("returned.")
			return
		end
		
		task.wait(.05/speed)
		
		if cPositionX < dPositionX then
			Part.Position += Vector3.new(.5, 0, 0)
		elseif cPositionX > dPositionX then
			Part.Position -= Vector3.new(.5, 0, 0)
		end
		
		if cPositionZ < dPositionZ then
			Part.Position += Vector3.new(0, 0, .5)
		elseif cPositionZ > dPositionZ then
			Part.Position -= Vector3.new(0, 0, .5)
		end
		
		currentPosition = Part.Position
		designatedPosition = Part2.Position

		cPositionX = currentPosition.X
		cPositionZ = currentPosition.Z

		dPositionX = designatedPosition.X
		dPositionZ = designatedPosition.Z
	end
end

return module

If I had to guess, this is probably just because the position will never be exactly like the other’s position. An easy solution to this is to stop the script when the two are near each other, like so:

if (position1 - position2).Magnitude <= 1 then -- replace variables with positions
   -- do stuff
end
1 Like

I tried doing this, this didn’t work at all, and It kept going.

	while currentPosition ~= designatedPosition do
		print((designatedPosition - currentPosition).Magnitude)
		if (designatedPosition - currentPosition).Magnitude == 2 then
			print("returned.")
			break
		end
		
		task.wait(.05/speed)
		
		if cPositionX < dPositionX then
			Part.Position += Vector3.new(.5, 0, 0)
		elseif cPositionX > dPositionX then
			Part.Position -= Vector3.new(.5, 0, 0)
		end
		
		if cPositionZ < dPositionZ then
			Part.Position += Vector3.new(0, 0, .5)
		elseif cPositionZ > dPositionZ then
			Part.Position -= Vector3.new(0, 0, .5)
		end
		
		currentPosition = Part.Position
		designatedPosition = Part2.Position

		cPositionX = currentPosition.X
		cPositionZ = currentPosition.Z

		dPositionX = designatedPosition.X
		dPositionZ = designatedPosition.Z
	end

The reason for this never stopped is because the NPC will simply never reach the designatedPosition because it is being updated too fast. Also, the magnitude could work.

Problem:

You said until it’s equal to 2, which by chance equal to 2 would be very unlikely, so just say <= 2 (less than or equal to 2) which should fix this problem.

1 Like