Pathfinding MoveTo script doesn't keep slow Humanoid on path, even with repeat

I have a path (not a straight line) that I want a character to follow, but my character is supposed to be slow, and my current pathfinding script will just skip the point it’s supposed to pathfind and move to the next point.

repeat
	task.wait()
until (script.Parent):IsA("Model")
local col = require(game.ServerStorage:WaitForChild("Collisions"))
local inunderground
local UndergroundStatusVal
local hum = script.Parent:WaitForChild("Humanoid")
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
if script.Parent.Name == "Umbra Monstrum" then
	inunderground = script.Parent:WaitForChild("IsUnderground")
	UndergroundStatusVal = hum.Parent:WaitForChild("UndergroundStatusVal")
end
local PointIndex
local CurrentPathPoint = hum.Parent:WaitForChild("CurrentPathPoint")
local hrpy = hrp.CFrame.Y
print(hrpy)
local path = script.Parent:WaitForChild("ChosenPath")
local pathway = game.Workspace:WaitForChild("PathWay")
local chosenPath = pathway:WaitForChild(path.Value)
local merged = pathway:WaitForChild("Merge")
local chosenchildren = chosenPath:GetChildren()
local mergedchildren = merged:GetChildren()
local pathpoints = {}
local undergroundpath = (game.Workspace:WaitForChild("UndergroundPathWay")):GetChildren()
col.SetPartToGroup(undergroundpath, "Paths and Doorways")
for i, v in pairs(chosenchildren) do
	table.insert(pathpoints, v)
end
for i, v in pairs(mergedchildren) do
	table.insert(pathpoints, v)
end
print(pathpoints)
PointIndex = 1
print(pathpoints[PointIndex])
local MoveToPos
local MoveToInst
if script.Parent.Name == "Umbra Monstrum" then
	UndergroundStatusVal.Changed:Connect(function(a)
		print("Gottem")
		print(a)
		if a == 1 then
			print("I DID IT")
			print(PointIndex)
			print(pathpoints[PointIndex])
			MoveToInst = pathpoints[PointIndex]
			print(MoveToInst)
			MoveToPos = MoveToInst.Position
			print(MoveToPos)
			hum:MoveTo(MoveToPos, MoveToInst)
		end
	end)
	inunderground:GetPropertyChangedSignal("Value"):Connect(function()
		if inunderground.Value == true then
			print(PointIndex)
			print(pathpoints[PointIndex])
			MoveToInst = pathpoints[PointIndex]
			print(MoveToInst)
			MoveToPos = MoveToInst.Position
			print(MoveToPos)
			hum.WalkSpeed = 25
			hum:MoveTo(MoveToPos - Vector3.new(0, -8, 0))
			task.wait(math.random(1, 2))
			hum.WalkSpeed = 0
			inunderground.Value = false
		end
	end)
end
hum.MoveToFinished:Connect(function()
	print(hrp.CFrame.Y)
	if script.Parent.Name == "Umbra Monstrum" then
		if UndergroundStatusVal.Value == 1 or inunderground.Value == true then --Goals for tomorrow (In case I forget) make monster move when it goes back up, use GetPropertyChangedSignal for UndergroundStatusVal
			PointIndex += 1
			print(pathpoints[PointIndex])
			MoveToInst = pathpoints[PointIndex]
			MoveToPos = MoveToInst.Position
			print(MoveToPos)
			if script.Parent.Name == "Umbra Monstrum" then
				if inunderground.Value == true then
					hum:MoveTo(MoveToPos - Vector3.new(0, -8, 0))
				else
					hum:MoveTo(MoveToPos, MoveToInst)
				end
			else
				hum:MoveTo(MoveToPos, MoveToInst)
			end	
		end
	else
		PointIndex += 1
		print(pathpoints[PointIndex])
		MoveToInst = pathpoints[PointIndex]
		MoveToPos = MoveToInst.Position
		print((hum.Parent.PrimaryPart.Position - pathpoints[PointIndex - 1].Position).Magnitude)
		if (hum.Parent.PrimaryPart.Position - pathpoints[PointIndex - 1].Position).Magnitude < 0.1 then
			hum:MoveTo(MoveToPos, MoveToInst)
		else
			repeat --This repeat loop is SUPPOSED to keep looping until the monster reaches its destination, but it doesn't.
				hum:MoveTo(pathpoints[PointIndex - 1].Position, pathpoints[PointIndex - 1])
				hum.MoveToFinished:Wait()
			until (hum.Parent.PrimaryPart.Position - pathpoints[PointIndex - 1].Position).Magnitude < 0.1
		end
	end
end)
hum:MoveTo(pathpoints[PointIndex].Position, pathpoints[PointIndex])

(My script is so messy :sweat_smile:)

Simpler explanation:





Hopefully you get it : )
Thank you for taking your time to read this

Are you trying to create a pathfind to a player or a pathfind to an object?

Edit: Looking at images, the pathfind is looking for the fastest possible route to the object, which is normal for pathfinding

Pathfind to an object, sorry for not explaining, the yellow dots in the above images are the points on the path (2 separate paths that merge later on)

This may be the video for you.

I dont have the time rn so here is a vid

1 Like

Ahh, GnomeCode (I started my game with his tutorials haha). I’ll go check it out later as I don’t really have much time here either

EDIT: After checking it out I realized that he made the path short enough for the zombie to reach the next waypoint without reaching the 8-second automatic finish, which is exactly what happens to me, hence the repeat in my code. (Which obviously doesn’t work because I’m bad at this)

The the reason its not working because he is using a for loop, and youre using a repeat loop, since they are both loops, you wont get a different result, to avoid the rig stopping in its tracks, you create waypoints using the PathfindingService. IF the rig gets all choppy and glitchy, use SetNetworkOwner on the rig, as it will make the choppy parts more smooth (You can use this when making a zombie, or a trail)

1 Like

The thing about my repeat loop is that it doesn’t use a variable that changes every iteration, so I expect it to keep trying to reach the next waypoint. and

PointIndex += 1

This line inside the MoveToFinished event function is supposed to act like i going up every iteration so the character moves and follows the waypoints.

While I was writing this I was coming up with a solution myself while waiting and I solved it (I think, I tested it once and it did work)

print((hum.Parent.PrimaryPart.Position - pathpoints[PointIndex].Position).Magnitude)
		if (hum.Parent.PrimaryPart.Position - pathpoints[PointIndex].Position).Magnitude < 2 then
			PointIndex += 1
			print(pathpoints[PointIndex])
			MoveToInst = pathpoints[PointIndex]
			MoveToPos = MoveToInst.Position
			hum:MoveTo(MoveToPos, MoveToInst)
		else
			repeat --This repeat loop is SUPPOSED to keep looping until the monster reaches its destination, but it doesn't.
				hum:MoveTo(pathpoints[PointIndex].Position, pathpoints[PointIndex])
				hum.MoveToFinished:Wait()
			until (hum.Parent.PrimaryPart.Position - pathpoints[PointIndex].Position).Magnitude < 0.1
		end

Thank you for trying to help though @DasKairo, I will be sure to try your solution out in the future if anything comes up.

@SimplySquidtastic Anytime my friend

1 Like