I would like to make it so that an npc attacks the person who attacks them. I can make it so that if the npc is damaged, they will check enemies within a greater radius, but this is a problem if players attack from very far. How would I accomplish this?
✓ ANSWER ACCEPTED
How would I make an npc damage the person that attacked them?
2 answers
✓ Accepted answer
You can add a 'tag' in the NPC that holds the player's name, then using the 'ChildAdded' function you can search for the player and target it. In your weapon:
local Tag = Instance.new("StringValue")
Tag.Name = "Damaged"
Tag.Value = Whatevertheplayeris.Name
Tag.Parent = WhatevertheNPCis
spawn(function()
wait(10) -- Expire after this time, so the NPC does not target the Player forever.
Tag:Destroy()
end)
In the NPC searching script:
WhatevertheNPCis.ChildAdded:Connect(function(child)
if child:IsA("StringValue") and child.Name == "Damaged" then
local Player = workspace:FindFirstChild(child.Value)
if Player then
-- Do whatever you do
end
end
end)
Thanks, it works.
This discussion is preserved as it was. New answers, comments, and votes are closed.
