How to make npc not hit player?

So I’m trying to make a skill for a certain character that summons a soldier/ an army. The problem is it hits me along with everyone else. How do I make it so that it only hits the other players and not hit me?

Server Script:

local RTorso = script.Parent.HumanoidRootPart
local Humanoid = script.Parent.Humanoid

local function findtarget()
	local aggro = 300
	local target = nil 
	for i, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local vtorso = v:FindFirstChild("HumanoidRootPart")
		if v.Name ~= "RomanSoldier" then
		if human and vtorso and v ~= script.Parent then
			
			
				
				if (RTorso.Position - vtorso.Position).magnitude < aggro then
				target = vtorso
           end			
		end
	end
 end
	return target
end	
	
	
	
while wait() do
	local torso = findtarget()
	if torso then
	Humanoid:MoveTo(torso.Position)
	end
end
	

Your script miss something:

local RTorso = script.Parent.HumanoidRootPart
local Humanoid = script.Parent.Humanoid

local function findtarget()
	local aggro = 300
	local target = nil 
	for i, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local vtorso = v:FindFirstChild("HumanoidRootPart")
		if v.Name ~= "RomanSoldier" then
		if human and vtorso and v ~= script.Parent then
			if (RTorso.Position - vtorso.Position).magnitude < aggro then
				target = vtorso
            end	
		end
	end
 end
	return target
end	
	
	
	
while wait() do
	local torso = findtarget()
	if torso then
	    Humanoid:MoveTo(torso.Position)
        Humanoid.MoveToFinished:Wait()
	end
end

also thats not the killing script, you should not fliter the target here, if you get the model from toolbox you should find the kill script in it

1 Like

It could probably because some parts have .CanCollide enabled.
You can disable this in the propities tab any part.
And in-game you can do this by simply doing part.CanCollide = false.

Hope that helps!
PD: Check what @NarakuLite said! That can probably help you fix some thigns inside your script. I just answered your question, not fixed your code :sweat_smile:

I can’t give you a script, but I can tell you exactly what you’ll need to do in order for the NPCs to recognize the difference between players and other NPCs.

You’ll need to use CollectionService and add one tag for all the players, and a different tag for all the NPCs. (i.e players would have the “friends” tag, and NPCs would have the “enemies” tag.

You can read more in depth in the article for CollectionService on the DevHub.

https://developer.roblox.com/en-us/api-reference/class/CollectionService

1 Like

Yeah I havent added the killing script yet because im still trying to make it so that they dont attack me before I add it

thanks I’ll try this out it sounds like it would work

1 Like

You can simply add a value inside the NPC like “creator” with the player’s name. Then have the attack script check if the target’s name is the creator’s name, if not it’ll damage. No need to do all of this.

1 Like

if you want it to be more accurate, you’ll have to use collectionservice.

Do you know how I can get the name if he is a random player?

how does the script know when to fire? is there a button that is detected on the client side and fires a remote event so the server script knows when to run?

I placed the script in the model so it activates automatically

Does the script run as soon as the player spawns in the game?

Well there technically isn’t a script/button to make the script run because the script already runs when the soldiers spawn in. But there is a script to make the soldiers spawn in since I keep them in replicated storage.

Thanks this was my first time using collection service but somehow it worked. Thanks so much.

1 Like