Need help with NPC's targetting other NPC

Hello!
I’ve been making a NPC that attacks a certain group of enemies. However, the friendly npcs attack eachother instead of attacking the hostiles.

My problem is, even though i scripted it, they’re still attacking eachother. I do not want this.

This isnt my full script, but just the part that isnt working.

local DontAttack = {"Farmer", "Knight", "Sentry", "King", "Cook", "NPC"}

function dmg(hit)
	if hit.Parent ~= nil then
		if (hit.Name ~= DontAttack) then
		local Player = Players:GetPlayerFromCharacter(hit.Parent)
		local hum = hit.Parent:FindFirstChild("Humanoid")
		if hum and not Player then
           --something
   end
end

Hitbox.Touched:Connect(dmg)

I tried to use a group to make it so the script isnt as messy, but it doesnt work.

Any help is appreciated.

Youre checking if hit.Name equals the table so it would be only true if hit.Name was {“Farmer”, “Knight”, “Sentry”, “King”, “Cook”, “NPC”}, for checking if a value is inside a table use table.find(),

local DontAttack = {"Farmer", "Knight", "Sentry", "King", "Cook", "NPC"}

function dmg(hit)
	if hit.Parent ~= nil then
		if not table.find(DontAttack, hit.Parent.Name) then --// i also changed hit.Name to hit.Parent.Name because it would be checking the bodyparts name
			local Player = Players:GetPlayerFromCharacter(hit.Parent)
			local hum = hit.Parent:FindFirstChild("Humanoid")
			if hum and not Player then
				--something
			end
		end

		Hitbox.Touched:Connect(dmg)
2 Likes

Thanks for the reply. I will try this.

It works. Thanks so much!!!

1 Like

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