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