How can I fix this move module?

I’m making a module that I can use so that I can move parts and get them to the same position.

I’m firing the Module’s function, but the part never changes position, even though all of the print events are firing.

  03:03:01.320  Server Script Ready!  -  Server - Move:6
  03:03:01.320  MoveTo()  -  Server - MoveModule:12
--\\ Module Made by Star \\--

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

local module = {}

function module:MoveTo(Part, Part2)
	print("MoveTo()")
	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
		task.wait(.5)
		
		if cPositionX < dPositionX then
			currentPosition += Vector3.new(.5, 0, 0)
		else if cPositionX > dPositionX then
				currentPosition -= Vector3.new(.5, 0, 0)
			end
		end
		
		if cPositionZ < dPositionZ then
			currentPosition += Vector3.new(.5, 0, 0)
		else if cPositionZ > dPositionZ then
				currentPosition -= Vector3.new(.5, 0, 0)
			end
		end
		
		currentPosition = Part.Position
		designatedPosition = Part2.Position

		cPositionX = Part.Position.X
		cPositionZ = Part.Position.Z

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

return module

Check if the loop is running by adding a breakpoint.

What’s a breakpoint?

You can just add a print statement inside the loop.
A breakpoint just stops the code when a certain point is reached.

As you can see, it never stops, there’s no finite pause.

I’m not sure what to do here, because visually, the part isn’t moving at all, even though I made sure to add to the position of the part.

There’re aren’t any errors that have been inside of the output, so I’m kind of in a tight position.

03:11:16.781  Server Script Ready!  -  Server - Move:6
  03:11:16.782  MoveTo()  -  Server - MoveModule:12
  03:11:20.671  1  -  Server - MoveModule:28
  03:11:21.177  2  -  Server - MoveModule:28
  03:11:21.691  3  -  Server - MoveModule:28
  03:11:22.206  4  -  Server - MoveModule:28
  03:11:22.724  5  -  Server - MoveModule:28
  03:11:23.239  6  -  Server - MoveModule:28
  03:11:23.756  7  -  Server - MoveModule:28
  03:11:24.274  8  -  Server - MoveModule:28
  03:11:24.788  9  -  Server - MoveModule:28
  03:11:25.289  10  -  Server - MoveModule:28
  03:11:25.805  11  -  Server - MoveModule:28
  03:11:26.324  12  -  Server - MoveModule:28
  03:11:26.840  13  -  Server - MoveModule:28
  03:11:27.356  14  -  Server - MoveModule:28
  03:11:27.873  15  -  Server - MoveModule:28
  03:11:28.391  16  -  Server - MoveModule:28
  03:11:28.906  17  -  Server - MoveModule:28
  03:11:29.422  18  -  Server - MoveModule:28
  03:11:29.937  19  -  Server - MoveModule:28
  03:11:30.454  20  -  Server - MoveModule:28
  03:11:30.971  21  -  Server - MoveModule:28
  03:11:31.487  22  -  Server - MoveModule:28
  03:11:32.003  23  -  Server - MoveModule:28
  03:11:32.507  24  -  Server - MoveModule:28
  03:11:33.023  25  -  Server - MoveModule:28
  03:11:33.541  26  -  Server - MoveModule:28
  03:11:34.059  27  -  Server - MoveModule:28
  03:11:34.581  28  -  Server - MoveModule:28
  03:11:35.088  29  -  Server - MoveModule:28
  03:11:35.605  30  -  Server - MoveModule:28
  03:11:36.123  31  -  Server - MoveModule:28
  03:11:36.639  32  -  Server - MoveModule:28
  03:11:37.140  33  -  Server - MoveModule:28
  03:11:37.655  34  -  Server - MoveModule:28
  03:11:38.173  35  -  Server - MoveModule:28
  03:11:38.673  36  -  Server - MoveModule:28
  03:11:39.189  37  -  Server - MoveModule:28
  03:11:39.706  38  -  Server - MoveModule:28
  03:11:40.206  39  -  Server - MoveModule:28
  03:16:05.813  557  -  Server - MoveModule:28

The part never moves because your code doesn’t change the part’s position, all it does is change the copy of the part’s position that you stored in the variables.

Also, it’s elseif not else if in Lua :slight_smile:

1 Like

Screenshot 2022-09-24 032912

What I meant is that you just have to do Part.Position = currentPosition after your calculations. Your function is fine, I’m not sure what the screenshot is supposed to show

1 Like

You need to remove the ends that were added for your else ifs

if cPositionX < dPositionX then
	currentPosition += Vector3.new(.5, 0, 0)
elseif cPositionX > dPositionX then
	currentPosition -= Vector3.new(.5, 0, 0)
end
1 Like

Now the script looks like this, but the part isn’t moving still.

function module:MoveTo(Part, Part2)
	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
	
	local times = 0
	
	while currentPosition ~= designatedPosition do
		times += 1
		task.wait(.5)
		
		print(times)
		
		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(.5, 0, 0)
		elseif cPositionZ > dPositionZ then
			Part.Position -= Vector3.new(.5, 0, 0)
		end
		
		currentPosition = Part.Position
		designatedPosition = Part2.Position

		cPositionX = currentPosition.X
		cPositionZ = currentPosition.Z

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

You’re adding to the X coord in your Z check

1 Like

It works now, thanks!