Tower Defense game zombies stop moving after a tower damages it

I’m pretty sure this in the right category.

  1. What do you want to achieve? Keep it simple and clear!
    The zombies should keep moving to every point even if they take damage.
  2. What is the issue? Include screenshots / videos if possible!
    Video
    The zombies that take damage walk to their next point, but then the point after that will result in it reaching the point then not going to the next.
  3. What solutions have you tried so far?
    Changing the way :MoveTo() is called. (including a instance argument, that didn’t work.)

Here is the pathfinding code for whoever needs it

local function moveTo(humanoid, targetPoint)
	local targetReached = false
	local connection
	connection = humanoid.MoveToFinished:Connect(function(reached)
		targetReached = true
		connection:Disconnect()
		connection = nil
	end)

	humanoid:MoveTo(targetPoint)

	while not targetReached do

		if not (humanoid and humanoid.Parent) then
			break
		end

		if humanoid.WalkToPoint ~= targetPoint then
			break
		end
		humanoid:MoveTo(targetPoint)
		task.wait()
	end


	if connection then
		connection:Disconnect()
		connection = nil
	end

end
-- thanks devhub!

script.Parent.Humanoid.Died:Connect(function()
	for i,v in pairs(script.Parent.Humanoid.Animator:GetPlayingAnimationTracks()) do
		v:Stop()
	end
	task.wait(4)
	for i,v in pairs(script.Parent:GetDescendants()) do
		if v:IsA("BasePart") or v:IsA("Decal") then
			game:GetService("TweenService"):Create(v,TweenInfo.new(1,Enum.EasingStyle.Quad,Enum.EasingDirection.InOut),{Transparency=1}):Play()
		end
	end
	task.wait(1)
	script.Parent:Destroy()
end)

local points = game.Workspace.PathPoints:GetChildren()

for i,v in pairs(points) do
	moveTo(script.Parent.Humanoid,v.Position)
end

local points = game.Workspace.PathPoints:GetChildren()

for i,v in pairs(points) do
	moveTo(script.Parent.Humanoid,v.Position)
end
game.Workspace.GAME_VALUES.BaseHealth.Value -= script.Parent.Humanoid.Health
script.Parent.Humanoid.Health = 0

I have no idea why the zombies taking damage would affect :MoveTo().
Thanks in advance for your help!

The problem is with the logic of the moveTo function. The while loop is meant to repeatedly call MoveTo until the target is reached. However, it doesn’t consider the situation where the zombie gets damaged and its WalkToPoint changes to the next target point while it’s still inside the while loop.

local function moveTo(humanoid, targetPoint)
	local targetReached = false
	local connection

	connection = humanoid.MoveToFinished:Connect(function(reached)
		targetReached = true
		connection:Disconnect()
		connection = nil
	end)

	humanoid:MoveTo(targetPoint)

	while not targetReached do
		if not (humanoid and humanoid.Parent) then
			break
		end

		if humanoid.WalkToPoint ~= targetPoint then
			humanoid:MoveTo(targetPoint)
		end

		task.wait()
	end

	if connection then
		connection:Disconnect()
		connection = nil
	end
end

-- Rest of the code...

Try this idk if it will work well

2 Likes

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