NPC should walk after attack if not near (Solutioned)

I’m making a NPC, but he’s supposed to follow the victim after attacking, the problem is when the cooldown starts, it’s blocking the follow after attack function until it’s finished.

Script:

local Configuration = script.Parent.Parent.Configuration

cd = false

script.Parent.TargetHumanoid:GetPropertyChangedSignal("Value"):Connect(function()
	while true do
		if script.Parent.TargetHumanoid.Value ~= nil then
			if (script.Parent.Parent:WaitForChild("Torso").Position - script.Parent.TargetHumanoid.Value.Parent.Torso.Position).Magnitude <= Configuration.Distance.Value and cd == false then
				cd = true
				local AttackAnim = script.Parent.Parent:WaitForChild("Enemy"):LoadAnimation(Configuration.Animations.AttackAnim)
				AttackAnim:Play()
				script.Parent.TargetHumanoid.Value:TakeDamage(Configuration.Damage.Value)
			else
				wait(Configuration.Cooldown.Value)
				cd = false
				print("a")
				script.Parent.Parent.Enemy.WalkToPoint = script.Parent.TargetHumanoid.Value.Parent.HumanoidRootPart.Position
			end
		end
		wait()
	end
end)

Had to use 2 scripts but works perfectly.

local Configuration = script.Parent.Parent.Configuration

while true do
	if script.Parent.TargetHumanoid.Value ~= nil then
		if (script.Parent.Parent:WaitForChild("Torso").Position - script.Parent.TargetHumanoid.Value.Parent.Torso.Position).Magnitude > Configuration.Distance.Value then
			script.Parent.Parent.Enemy.WalkToPoint = script.Parent.TargetHumanoid.Value.Parent.HumanoidRootPart.Position
		end
	end
	wait()
end
1 Like