I'm trying to make a script, so that when the NPCis attacked by a Player the NPC attacks the player. Ive got an on changed script that calls the function "Find". But i dont know how to find players in the workspace? And if its possible is there a way to make it find the exact player who attacked the NPC?
function Find() local player = game.Workspace:FindFirstChild("?") local torso = player.Torso script.Parent.RocketPropulsion.Target = torso end
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 --Checks to make sure it is an NPC local tag = instance.new("StringValue") -- Creates the tag tag.Value = Player.Name -- Tells what player attacked NPC tag.Parent = part.Parent.Humanoid part.Parent.Humanoid:TakeDamage(50) -- Adjust end end)
In NPC.Humanoid (Normal Script)
script.Parent.ChildAdded:connect(function(tag) --Fired whenever an item (tag) is added to humanoid local Attacker = game.Players:GetPlayerFromCharacter(tag.Value) --Gets the attacker if tag:IsA("StringValue") and Attacker then --Makes sure the tag is a tag, and that the player exists Humanoid: WalkToPart(Attacker.Character.Torso) --Walks to Player 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.