How to make npc attack back?

Hello.
I recently made a npc that attacks you while near it. Then I also added a tool for the npc to equip while attacking the player. Here is a picture of it.

Picture

image

What I am trying to do from here is that when the npc gets hit it attacks the player

I’ve tried to check for some ways to script this but no sources explained how to do it.

Help needed.

4 Likes

you can do if hit.Parent.Parent.Parent:FindFirstChild("Humanoid") then when a person attacks. Assuming the script is a child of the tool part and the tool part is the child of the tool and the tool is the child of the players’ character

This is the script

 local Anim = Humanoid:LoadAnimation(Animation)
    function Hit(hit)
    	local TargetHum = hit.Parent:FindFirstChild("Humanoid")
    	script.Parent:WaitForChild("Sword").Trail.Enabled = true
    	if TargetHum ~= nil and TargetHum:IsA("Humanoid") and AttackEnabled == true and TargetHum ~= Humanoid then
    		AttackEnabled = false
    		delay(0.5,function() AttackEnabled = true end)
    		if Anim then Anim:Play(nil,nil,1.5) end
    		script.Parent:WaitForChild("Sword").Trail.Enabled = false
    		DamageTag(TargetHum.Parent,5)
    		Hit1:Play()
    		Hit2:Play()
    	end
    end
1 Like

swords generally leave a tag, a ValueInstance, in the character that they damage. This is normally used by leaderboard systems to determine who killed a character. You can use this to determine who attacked the NPC. Then, you can using the pathfinding service to try and walk towards the player while slashing. This is the most common method used in RPG games.

To get more advanced you could integrate my new sword fighting AI into your game. I described it here:

It doesn’t do any pathfinding, jumping, or slashing yet though and you would need to modify the movement to work with your NPC models / system. Shouldn’t be too hard. I also never got around to serializing the Q-table but a quick call to the HttpService EncodeJSON / DecodeJSON should handle that well. This would be a very deadly AI to get near once fully trained!

3 Likes

you could add a value in the NPC, the weapon of the player will have a line that if your hit an NPC and it has that value, the value will be set to true, and a script in the NPC will identify if the value is false, if it is not false then the action of attack the player back will be true, if you want a script then maybe it will take a week to do cause im not good scripting weapons.

I need to see the parenting, maybe send the screenshot?

Here is the screenshot.

I will explain in a way that I would prefer to do it.

Bindable Events

There is a useful thing called BindableEvents. It is basically a remote event for same boundary communication. (server → server, same client → same client)

You can use these bindable events for signaling specific NPC’s that they need to attack someone when they are attacked. In your attack script you could invoke the aggro bindable of a the npc that is being attacked.

1 Like

Oops, forgot to specify, I just need the tool one

1 Like

I’m not sure why I can’t post, but also have the same question, but when I scrolled through this, I found no answer. Can somebody answer this? I have little scripting knowledge and I’m trying to make NPC’s like Entry Point’s.

:dizzy_face:

3 Likes

There are several ways to do this. This topic has a lot of views, and people are still trying to find an answer (sorry for the long wait, @ExiledVoyager). @soccervr, if this post is a solution, I suggest marking it, so others may find what they need.

Let’s check when the NPC health changes, shall we? When the NPC is attacked, it’s health changes, and it will attack. We need a script, set up inside the NPC. A simple function will do:

local hum = script.Parent.Humanoid
local tool = script.Parent.Tool
			
local function GetNearestCharacter(ComparePart) -- function for getting nearest character. Called when NPC damaged
	local character, magnitude = nil, -1
	for _, player in ipairs(Players:GetPlayers()) do
		if player.Character and (player.Character.PrimaryPart.Position - ComparePart.Position).Magnitude > magnitude then
			character, magnitude = player.Character, (player.Character.PrimaryPart.Position - ComparePart.Position).Magnitude
		end 
	end
	return character,magnitude
end

hum.HealthChanged:Connect(function()
	local Nearest = GetNearestCharacter(AI.PrimaryPart)
	--Now, activate the tool
	tool:Activate()
	--Have the NPC chase the player now. This code will be up to you.
    --the "Nearest" variable is the character that is nearest to the NPC - the one it will be attacking.
end)