✓ Accepted answer
One common way you could do this, would be to use a 'tag' system. This is a method in which the weapon leaves a "tag" (a stringValue or something with a players name as the value) on the NPC, or whoever was attacked.
This is pretty simple method, the only drawback is that you have to add this into every single weapon that is in the game for it to work 100%.
Here's an example of how that would work:
In The Weapon (LocalScript)
local Player= game.Players.LocalPlayer
bullet.Touched:connect(function(part)
if part.Parent:FindFirstChild("Humanoid") and not game.Players:GetPlayerFromCharacter(part.Parent) then
local tag = instance.new("StringValue")
tag.Value = Player.Name
tag.Parent = part.Parent.Humanoid
part.Parent.Humanoid:TakeDamage(50)
end
end)
In NPC.Humanoid (Normal Script)
script.Parent.ChildAdded:connect(function(tag)
local Attacker = game.Players:GetPlayerFromCharacter(tag.Value)
if tag:IsA("StringValue") and Attacker then
Humanoid: WalkToPart(Attacker.Character.Torso)
end
end)
Just remember that this is only an example and you'll have to edit it to fit in with your script however you need. If you need help with that, let me know.
Anyways, I hope this helped. If not, or if you have any further problems/questions, please leave a comment below, and I'll see what I can do.