How would i make these enemy move after attacking?

  1. What do you want to achieve?
    Tried to make a complete code of unit/enemy code like battle cats, but fixing bugs was already painful and sorry for the kind of messy code

  2. What is the issue?
    NPCs (ie. enemy) not moving after attack. Couldnt find the fix though
    It was supposed to not attack after attacking cause it had a timer so it can walk and idle until the timer is off

  1. What solutions have you tried so far?
    it did worked before i added the task.delay when i added onHit and debounce3 toif atktimer >= 0 then elseif (except with multiple attack bug and sometimes it doesnt walk after atk). Tried to add elsed bool in if not animation['atk'].IsPlaying then and that still doesnt work…

Here’s the lua code, though its confusing ig

local npc = script.Parent
local stats = require(npc.Stats)

local baseunit = game.Workspace.Enviroment["Base-unit"]
local baseenemy = game.Workspace.Enviroment["Base-Enemy"]

local hitbox = Instance.new("Part")
local weld = Instance.new("WeldConstraint")

weld.Part0 = npc:WaitForChild("HumanoidRootPart")
weld.Part1 = hitbox; weld.Parent = hitbox

hitbox.Name = 'Hitbox'
hitbox.Anchored = false; hitbox.CanCollide = true
hitbox.Massless = true
hitbox.CanCollide = false
hitbox.Transparency = 0.5
hitbox.Size = Vector3.new(3.2, 5.8, 3.8)
hitbox.CFrame = (npc.HumanoidRootPart.CFrame + Vector3.new(0, 0, -hitbox.Size.Z *0.5))
hitbox.Parent = npc

local animation = {
	['idle'] = npc.Humanoid:LoadAnimation(npc.Idle),
	['run'] = npc.Humanoid:LoadAnimation(npc.Run),
	['atk'] = npc.Humanoid:LoadAnimation(npc.Attack)
}

local animstring = 'run'

function updateAnim(string)
	if not animation[string].isPlaying then
		for i,v in pairs(animation) do
			if i ~= string then
				animation[i]:Stop()
			end
		end
		animation[string]:Play()
	end
end

local curwalk = npc.Humanoid.WalkSpeed 
local elsed = false

local atktimer = 0
local idletimer = 0
local debounced1 = false
local debounced2 = false
local debounced3 = false

function onHit()
	--if atktimer <= 0 then
		--if not debounced3 then
			local unitfolder = game.Workspace.Unit
			for i,entity in pairs(unitfolder:GetChildren()) do
				local relativePos = hitbox.CFrame:PointToObjectSpace(entity.HumanoidRootPart.Position)
				if npc.Humanoid.Health > 0 and math.abs(relativePos.X) <= npc.Hitbox.Size.X * 1 * 0.6 and math.abs(relativePos.Z) <= npc.Hitbox.Size.Z * 1 * 0.6 then
					entity.Humanoid:TakeDamage(stats.atk)
					print(tostring(entity.Name)..' HIT!')
				end
			end
		--end
	--end
end

function update()
	if npc.Humanoid.Health <= 0 then
		task.wait(2)
		npc.Parent = nil
	end

	npc.Humanoid:MoveTo(baseunit.Position + Vector3.new(5,0,0))
	if not animation['atk'].IsPlaying then
		debounced2 = true
	end

	if animation['run'].IsPlaying then
		npc.Humanoid.WalkSpeed = curwalk
	elseif animation['idle'].IsPlaying or animation['atk'].IsPlaying then
		npc.Humanoid.WalkSpeed = 0
	end
	if not elsed then
		if npc.HumanoidRootPart.Velocity.Magnitude >= 0.004 then
			animstring = 'run'
		else
			animstring = 'idle'
		end
	end

	local unitfolder = game.Workspace.Unit
	for i,entity in pairs(unitfolder:GetChildren()) do
		local relativePos = hitbox.CFrame:PointToObjectSpace(entity.HumanoidRootPart.Position)
		if not debounced3 and entity.Humanoid.Health > 0 then
			if math.abs(relativePos.X) <= npc.Hitbox.Size.X * 1 * 0.6 and math.abs(relativePos.Z) <= npc.Hitbox.Size.Z * 1 * 0.6 then
				if not debounced1 then
					atktimer = stats.atktimer
					idletimer = stats.idletimer
					animstring = 'atk'
					debounced2 = false
					elsed = true
					debounced1 = true
					task.delay(atktimer,function()
						onHit()
						debounced3 = true
					end)
				end
			end
		elseif idletimer >= 0 and debounced2 and math.abs(relativePos.X) <= npc.Hitbox.Size.X * 1 * 0.6 and math.abs(relativePos.Z) <= npc.Hitbox.Size.Z * 1 * 0.6 then
			animstring = 'idle'
		end
	end
	if atktimer >= 0 then
		atktimer -= 0.015
	end
	if idletimer >= 0 and debounced2 then
		elsed = false
		idletimer -= 0.015
		debounced1 = false
	elseif idletimer <= 0 then
		debounced3 = false
	end
	updateAnim(animstring)
end

while task.wait() do
	update()
end

I don’t see any visible errors, but I do see quite a bit of things that may be responsible for your current problem or future ones.

You have a lot of if statements, I would go back and make sure that each operation is the correct value.

You have a loop constantly calling your update() function, this may create issues with threading.

I have noticed that some of your debounces are not always set to the correct value until later. I also noticed that you are using if not debounce then and, from past experience, this will sometimes only check if the value is nil instead of false.

In the future, I would recommend using Pathfinding, rather than, :MoveTo().

Thanks for the advice though! Would try to rewrite the script again but any advice to make the code lines (ie. ifs and debounced bools) be more cleaner?

If I’m being honest, my code isn’t that much better, but, one piece of advice, create variables for equations that will be used in if statements, makes it easier to read, in my opinion.