Help with Enemy NPC

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a barbarian attack when a player or hostile npc is within it’s vicinity.

  2. What is the issue? Include screenshots / videos if possible!
    The npc won’t change his animation nor go to the player/hostile npc.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried printing to debug, but as far as I can see it should be working.


My Script;

local NPC = script.Parent.Humanoid
local WayPoints = game.Workspace.BWP:GetChildren()
local Agro = false

local function Walk()
	local RandomWayPoint = math.random(1, #WayPoints)
	local ChosenWayPoint = WayPoints[RandomWayPoint]

	NPC:MoveTo(ChosenWayPoint.Position)
	NPC.MoveToFinished:Wait(2)
end

game.Workspace.Village.Agro.Touched:Connect(function(hit)
	print(hit.Name)
	if not hit.Parent.Name == "Barbarian" or not hit.Parent.Name == "BarbarianSoldier" and hit.Parent:FindFirstChild("Humanoid") then
		Agro = true
		NPC:MoveTo(hit.Position)
	end
end)

game.Workspace.Village.Agro.TouchEnded:Connect(function(hit)
	print(hit.Name)
	if not hit.Parent.Name == "Barbarian" or not hit.Parent.Name == "BarbarianSoldier" and hit.Parent:FindFirstChild("Humanoid") then
		Agro = false
	end
end)

while Agro == false do
	wait(2)
	Walk()
end

My Animation Script

local AnimTrack = script.Parent.Humanoid.Animator:LoadAnimation(script.Animation)
local AnimTrack2 = script.Parent.Humanoid.Animator:LoadAnimation(script.Animation2)
local AnimTrack3 = script.Parent.Humanoid.Animator:LoadAnimation(script.Animation3)

--Walk Anim
script.Parent.Humanoid.Running:Connect(function(speed)
	if speed > 0 and not AnimTrack.IsPlaying then
		AnimTrack:Play()
	elseif speed == 0 and AnimTrack.IsPlaying then
		AnimTrack:Stop()
	end
end)

--Shield Up Anim
game.Workspace.Village.Agro.Touched:Connect(function(hit)
	if not hit.Parent.Name == "Barbarian" or not hit.Parent.Name == "BarbarianSoldier" and hit.Parent:FindFirstChild("Humanoid") then
		if not AnimTrack2.IsPlaying then
			AnimTrack2:Play()
		end
	end
end)

game.Workspace.Village.StopAgro.Touched:Connect(function(hit)
	if not hit.Parent.Name == "Barbarian" or not hit.Parent.Name == "BarbarianSoldier" and hit.Parent:FindFirstChild("Humanoid") then
		if AnimTrack2.IsPlaying then
			AnimTrack2:Stop()
		end
	end
end)

--Swing Anim
local Cooldown = 1
local debounce = false

script.Parent.SwingDist.Touched:Connect(function(hit)
	if not hit.Parent.Name == "Barbarian" or not hit.Parent.Name == "BarbarianSoldier" and hit.Parent:FindFirstChild("Humanoid") then
		if not AnimTrack3.IsPlaying and not debounce then
			debounce = true
			AnimTrack3:Play()
		elseif not AnimTrack3.IsPlaying and debounce then
			wait(Cooldown)
			debounce = false
		end
	end
end)

The npc is a custom rig.

my best guess is touched and touchended are occurring repeatedly at the same time

OK, thanks! I’ll try this out when I get a chance!

It didn’t change anything. I added a print statement to fire once the script got inside of the if statement that detects if its not a friendly and has a humanoid and that didn’t go off so I’ve narrowed it down to one line on each function, but I can’t figure out how to fix that.

also want to point out that

should be

if not hit.Parent.Name == "Barbarian" and hit.Parent:FindFirstChild("Humanoid") or not hit.Parent.Name == "BarbarianSoldier" and hit.Parent:FindFirstChild("Humanoid") then

otherwise anything that isn’t named Barbarian would start the function (say for example an arrow being propelled through bodymovers that shouldn’t be attackable anyway)

1 Like

Thanks! I fixed the npc now. I needed to change this;

To this;

if hit.Parent.Name ~= "Barbarian" and hit.Parent.Name ~= "BarbarianSoldier" and hit.Parent:FindFirstChild("Humanoid") then
1 Like