Character bypasses a waypoint?

  1. What do you want to achieve?
    I’m currently trying to create a tower defense like game via a tutorial, I’ve gotten the enemy movement down. The enemies SHOULD walk to each waypoint until it reaches the end. There are only 2 enemies, the Noob and the Heafty.

  2. What is the issue?
    The Heafty bypasses waypoint 5 and goes off path. It’s strange and I don’t know a fix!

  3. What solutions have you tried so far?
    I haven’t tried much, couldn’t find anyone else with the problem.

Module script that manages movement, animations, etc.

local ServerStorage = game:GetService("ServerStorage")
local PhysicsService = game:GetService("PhysicsService")
local mob = {}

function mob.Move(entityName, map, animationName)
	local humanoid = entityName:WaitForChild("Humanoid")
	local waypoints = map.waypoints
	local normalWalk = game.ReplicatedStorage[animationName]

	for waypoint=1, #waypoints:GetChildren() do
		humanoid:MoveTo(waypoints[waypoint].Position)
		local loaded = humanoid:LoadAnimation(normalWalk)
		loaded.Looped = true
		loaded:Play()
		humanoid.MoveToFinished:Wait()
		loaded:Stop()
	end
	
	entityName:Destroy()
end


function mob.spawnEntity(entityName, amnt, map, animationName)
	local mobExists = ServerStorage.entities:FindFirstChild(entityName)
	
	if mobExists then
		for i=1, amnt do
			wait(0.5)
			-- Drag the mob selected into the workspace.
			local newMob = mobExists:Clone()
			newMob.HumanoidRootPart.CFrame = map.Path.ENTRANCE["Spawn-Brick"].CFrame
			newMob.Parent = workspace.ExistingMobs
			newMob.HumanoidRootPart:SetNetworkOwner(nil)
			for i, object in ipairs(newMob:GetDescendants()) do
				if object:IsA("BasePart") then
					PhysicsService:SetPartCollisionGroup(object, "Mob")
				end
					
					
			end
			
			coroutine.wrap(mob.Move)(newMob, map, animationName)
		end
		

	else
		warn("Could not grab entitiy --> ", entityName)
	end
end

return mob

Main script that uses the module script

local mob = require(script.Mob)
local map = workspace["Seemless Plains"]



for wave=1,10 do
	print("Wave is starting -->", wave)
	if wave <= 2 then
		mob.spawnEntity("Noob", 4, map,"normals-static")
	elseif wave >= 5 then
		mob.spawnEntity("Noob", 8, map,"normals-static")
	elseif wave <=7 then
		mob.spawnEntity("Heafty", 2, map, "normals-static")
		mob.spawnEntity("Noob", 4, map,"normals-static")
	elseif wave == 10 then
		mob.spawnEntity("Heafty", 5, map, "normals-static")
	end
	
	repeat
		task.wait(5)
	until #workspace.ExistingMobs:GetChildren() == 0
end

If there are any other detail I can provide then please, let me know! I’ll respond whenever I’m free, thank you!

2 Likes

Did you try printing to make sure each item is actually looping through? It could be an issue with the humanoid and it failed to MoveTo.

1 Like

I have just tried that and when it is about to hit waypoint 5 and bypasses it, it does say it has moved to it.

1 Like

Try repeating the MoveTo in a loop until its close enough to the waypoint, the default humanoid events can randomly stop based on the distance, it cant go too far or it just quits halfway through.

1 Like

What is the Name of the skipable waypoint (and the others)

Hi! Your issue is that your waypoints are out of order in the folder, and therefore loading in that order when the server starts. One possible fix is to name your nodes with numbers and then change your movement loop to this:

for waypoint=1, #waypoints:GetChildren() do
	wp = waypoints:FindFirstChild(waypoint)
	if wp == nil then error("No such waypoint") end
	humanoid:MoveTo(wp.Position)
	local loaded = humanoid:LoadAnimation(normalWalk)
	loaded.Looped = true
	loaded:Play()
	humanoid.MoveToFinished:Wait()
	loaded:Stop()
end

Try this and tell me how it works :slight_smile:

They are just numbers (1, 2, 3, 4, 5, 6, 7).

This sadly didn’t work. The error didn’t appear either. The nodes are numbered 1 to 7.

Is number 5 in the waypoints folder?

Yep, the other enemy go to waypoint 5 just fine.

I’ve found a fix! The issue was that MoveTo() has a 8 second timeout. Meaning if the humanoid hasn’t reached the designated goal location within 8 seconds, it skips it. Thanks to this post!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.