How can I see if a humanoid belongs to a player or NPC?

Hello Developers! I need help with detecting if a humanoid belongs to a player or NPC. I need this so that my gun can only attack NPC’s and not Players.

Here is the part of my script which does damage:

local function _defaultDamageCallback(system, target, amount, damageType, dealer, hitInfo, damageData)
	if target:IsA("Humanoid") then
		target:TakeDamage(amount)
		
	end
end

What edit do I have to make so that it can only attack NPCs?

2 Likes

You could check if game.Players:FindFirstChild(targetName) or you could store all of your NPCs in a folder in workspace and check if the humanoid’s parent is in that folder.

You can use Players:GetPlayerFromCharacter and give it the parent of the humanoid. If it returns a player, it is a humanoid of a player’s character (Or do what @NicholasY4815 said).

This is great but how can I do this in my code? Can you help me please?

You’ll just need an addition to your if statement.

if target:IsA("Humanoid") and game.Players:GetPlayerFromCharacter(target.Parent) then
9 Likes

Ok thanks alot! I will mark your post as solution.

1 Like