Alternative methods to Humanoid:MoveTo

Hello developers,

I am currently in the process of making a tower defense type game, and I have noticed that the path the characters are taking are are sort of going of path. I am currently using a waypoint system. Here’s a video of the issue occuring:

I want the character to full reach the destination and go in a straight line.

1 Like

You probably just need to adjust the distance check to the waypoint before going to the next or simply add a wait in there if you post the code maybe able to look at it. the wait will let them get all the way to the point before going to the next. Another way to move them is by using tweens with tween.Completed:Wait() then go to the next point with tween

3 Likes

Thanks for the extra info. Here is the enemy spawn code:

for i = 1, amount do
		task.wait(0.5)
		
		local CloneOfRequestedEnemy = RequestedEnemy:Clone()
		local MapTroopSpawnCFrame = MapTroopSpawn.CFrame
		local CFrameToMoveTo = CFrame.new(MapTroopSpawnCFrame.X, MapTroopSpawnCFrame.Y + 10, MapTroopSpawnCFrame.Z)

		CloneOfRequestedEnemy.HumanoidRootPart.CFrame = CFrameToMoveTo
		CloneOfRequestedEnemy.Parent = workspace.EnemiesFolderGame
		
		for index, value in CloneOfRequestedEnemy:GetDescendants() do
			if value:IsA("BasePart") then
				value.CollisionGroup = "Enemy"
			end
		end
		
		CloneOfRequestedEnemy.HumanoidRootPart:SetNetworkOwner(nil)

		task.defer(function()
			Package.MoveEnemyThroughWaypoints(CloneOfRequestedEnemy, mapToSpawnOn)
		end)
	end

Here is the code that moves the enemy to each waypoint:

function Package.MoveEnemyToPoint(enemyHumanoid: Humanoid, waypointToMoveTo: Part)
	enemyHumanoid:MoveTo(waypointToMoveTo.Position)
	enemyHumanoid.MoveToFinished:Wait()
end

Using Tweens isn’t exactly a solution as they do not play animations, and are not as configurable.

I think the problem is that MoveTo does not respect Scale, which I am using to scale down the enemies. I think the best idea would be to add an offset of the enemy’s position the way they are looking.

yeah you should try a small wait after the movetofinished before going to the next point this should allow them to almost walk directly onto the point

Yeah I just tried this, they just stop in front of the point where they are supposed to go. I am going to try my previous solution and see if that works. I am just not sure how I would get the orientation they are looking at and move them forward a bit.

yeah you could also do this. not sure on if it changes alot on scale with waypoint use. I know it does effect the humanoids physics and speed if scaled enough either way

Ok moving them forward a bit worked, since the scale is 0.25 I had to move the character forward 0.75 studs which means that scale was the issue.

The only issue is that I am using this code to move them forward:

enemyCharacter:PivotTo(enemyCharacter:GetPivot() * CFrame.new(0, 0, -0.75))

Which works, but it looks terrible in game because they just teleport forward 0.75 studs. How would I make them move smoothly?

I think with TweenService. You calculate the speed and their destination to get an approximate time for them to reach to the destination.

Wouldn’t this start to get laggy when you are on larger maps? (ex: 100 enemies at one time)

If that’s the case, then I would have the client tween the enemies on reaching their destination while the server teleports the enemies exactly to their destination through remote event. It’s gonna be server-to-client, so exploiters can’t really do anything about that.

can you not do another moveto for the offset location? and other than that to make things smooth you might could use AlignPosition and AlignOrientation those are the only other ways of moving and these can also be used for up to about 1k pets in a game at one time controlled by one client

How would this work exactly? If it teleports on the server, wouldn’t it just replicate to the client and play the tween after it teleports?

could you just offset your waypoints or are they auto generated?

The server tells the client their current position before teleporting them, so the client teleports the enemy back to their original position, and tween it.

1 Like

I see, that makes sense it seems a lot easier.

1 Like

Wouldn’t a better idea be just tweening on the client first, then teleporting the enemy on the server?

That also works, it kinda depends on the circumstances, but you can give it a try.

1 Like

I just noticed this. I do not recommend assigning a name to index in for loops if you’re not gonna use it. This can cause memory leaks, therefore, a bad practice. Many programmers commit this mistake, and professionals are aware of it.

Instead, assign it with underscore, in other words, a dash (_). It does nothing other than mark it as unused variable, which saves memories.

1 Like