Stop Npc moving to attack

I am trying to create an Npc that goes to a location and when something is close enough to its path, it will stop moving to attack it. I tried to run it using MoveTo and corutines, but it often started to break and the Npcs would get stuck

1 Like

yeah i feel your struggle with npcs. I’m familiar with MoveTo and how weird it can be, same for MoveToFinished. I think i’d need more information as to how your code works before i can give a good answer. From what i do know all i can say is to run frequent checks on your AI and what state its in. If youre using the waypoints system from PathFindingService then you should be careful with having the AI try to walk along two different sets of waypoints at the same time because the function was called two separate times at the same time.

When the Npc is Cloned and put into workspace, it gets MoveTo to the route I set. Then I use a raycast to check if there is anything infront of it and it should damage whatever humanoid is infront of it. When the humanoid is gone, the movement will resume. Also the route the Npc is taken is straight.

This is my last saved attempt of trying to attempt this

	
	if Enemy then
		task.wait()
		if Enemy:FindFirstChild("Humanoid") then
	local Human = Enemy:WaitForChild("Humanoid")

			
	local Move = coroutine.create(function()
				Human:MoveTo(workspace.EnemyDestination:FindFirstChild(Lane).Position)
				Human.MoveToFinished:Wait()
			end)
			
			RunService.Heartbeat:Connect(function()
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {workspace.Enimies,workspace.End}
			raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
			if Enemy:FindFirstChild("HumanoidRootPart") then
	local RayResult = workspace:Raycast(Enemy:WaitForChild("HumanoidRootPart").Position,Vector3.new(6,0,0),raycastParams)
			
				if RayResult ~= nil then
					
			
		local hitPart = RayResult.Instance
					if hitPart.Parent.Parent == workspace.Npcs  then 
						coroutine.yield(Move)
				local Human = hitPart.Parent
			
						if Human:FindFirstChild("Humanoid").Health >= Damage then
							
				Human:Destroy()
						else
							
				local sfx = Instance.new("Sound")
				sfx.Parent = Human.Torso
				sfx.SoundId = "rbxassetid://9082316300"
				sfx.PlaybackSpeed = 0.7
				RepStore.Events.Attack:FireAllClients(Enemy,"Punch")
				Human:FindFirstChild("Humanoid"):TakeDamage(Damage)
				sfx:Play()
				task.wait(0.5)
				sfx:Destroy()
							end 
						end
					end
				end  coroutine.resume(Move) 
			end) 
		end
		
end

describe how the ai breaks? error messages, or unexpected behaviors

You could run an external while loop with task.spawn or coroutines, check for the target’s distance between the player, then when it is close enough, attack and change the WalkSpeed of the humanoid to 0 for a while (you need to also use a debounce for this).

Here’s an idea on how the code will look like:

local debounce = false
local target --INSERT TARGET
local HRP --INSERT MONSTER HRP
local hum --INSERT MONSTER HUM

task.spawn(function()
	while true do
		if debounce == false then
			if target then
				local magnitude = (target.Position - HRP.Position).Magnitude

				if magnitude < 5 then
					target.Humanoid.Health -= 100
					task.defer(function()
						debounce = true
						hum.WalkSpeed = 0

						task.wait(2) -- COOLDOWN

						hum.WalkSpeed = 16
						debounce = false
					end)
				end
			end	
		end	
		task.wait()
	end
end)

I recommend using SimplePath Module.

I found a solution, thanks for the help