NPC Attack Script not Working

Hello! I am making an attack script that attacks the player whenever the player attacks the NPC. I made the script and all but when I test it out and attack the guard, it doesn’t even move. All it says is, “No attacker found”. I made it print stuff to help me find out what’s the problem. Here is the code.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PathFindingService = game:GetService("PathfindingService")
local players = game:GetService("Players")
local sword = script.Parent:WaitForChild("ClassicSword")
local be = script.Parent:WaitForChild("ToolBe")

-- Variables
local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")

local guardTemplate = ReplicatedStorage:WaitForChild("Guard") -- well... this is pretty self-explanatory
guardTemplate.HumanoidRootPart.CFrame = CFrame.new(-45, 13.141, -66)

char.Humanoid:UnequipTools()

local lastAttacker = Instance.new("ObjectValue")
lastAttacker.Name = "LastAttacker"
lastAttacker.Parent = hum

local function SwingSword(npc, player)
	local character = player.Character
	if character then
		be:Fire()
	end
end

local function Move(npc, attackerCharacter)
	local npcHumanoid = npc:FindFirstChild("Humanoid")
	local npcRoot = npc:FindFirstChild("HumanoidRootPart")
	local attackerRoot = attackerCharacter:FindFirstChild("HumanoidRootPart")

	if npcHumanoid and npcRoot and attackerRoot then
		while attackerCharacter and attackerCharacter:FindFirstChild("Humanoid") and attackerCharacter.Humanoid.Health > 0 do
			local path = PathFindingService:CreatePath({
				AgentRadius = 2,
				AgentHeight = 5,
				AgentCanJump = true,
				AgentJumpHeight = 7,
				AgentMaxSlope = 45
			})

			path:ComputeAsync(npcRoot.Position, attackerRoot.Position)

			if path.Status == Enum.PathStatus.Complete then
				local waypoints = path:GetWaypoints()
				for _, waypoint in ipairs(waypoints) do
					npcHumanoid:MoveTo(waypoint.Position)

					local reached = npcHumanoid.MoveToFinished:Wait()
					if reached and (npcRoot.Position - attackerRoot.Position).Magnitude <= 3 then
						SwingSword(npc, players:GetPlayerFromCharacter(attackerCharacter))
						wait(1)
						break
					end
				end
			else
				warn("Pathfinding failed for " .. npc.Name)
				break
			end

			wait(0.5)
		end
	end
end

local function onDamaged(attacker)
	if attacker then
		local attackerPlayer = players:GetPlayerFromCharacter(attacker)
		if attackerPlayer then
			print(attackerPlayer.Name .. " attacked the NPC.")
			char.Humanoid:EquipTool(sword)
			Move(char, attacker)
		end
	end
end

hum.HealthChanged:Connect(function()
	if hum.Health < hum.MaxHealth then
		if lastAttacker.Value then
			print("Health dropped! Attacker:", lastAttacker.Value.Name)
			onDamaged(lastAttacker.Value)
		else
			print("No attacker detected.")
		end
	end
end)
4 Likes

In “onDamaged” you never set “lastAttacker.Value” to the player?

1 Like

ohhhh, let me test it out real quick

The value isn’t being assigned to anything.

So if you add lastAttacker.Value = attackerPlayer after the if statement on line 70, does that work for finding a player in the onDamaged function?

ahhh, I did attackerplayer.name

No, on line 81, it needs lastattacker.value

Im not talking about print statements, on line 80 you check if lastAttacker has a value which will always be false since you never set the value to anything. So you need to set the value somewhere at some point or it will continue to forever be nil

local function onDamaged(attacker)
	if attacker then
		local attackerPlayer = players:GetPlayerFromCharacter(attacker)
		if attackerPlayer then
			print(attackerPlayer.Name .. " attacked the NPC.")
			char.Humanoid:EquipTool(sword)
			Move(char, attacker)
			
			script.Parent:WaitForChild("LastAttacker").Value = attackerPlayer
		end
	end
end

hum.HealthChanged:Connect(function()
	if hum.Health < hum.MaxHealth then
		if lastAttacker.Value then
			print("Health dropped! Attacker:", lastAttacker.Value.Name)
			onDamaged(lastAttacker.Value)
		else
			print("No attacker detected.")
		end
	end
end)

I have a last attacker var btw I just didn’t use it for the later attacker thing for some reason

Like to set the lastattacker value

Nvm, I fixed it. Thank you though!

1 Like

Nvm, it doesn’t work for some reason

I got it working now (:::::::::::::::::

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