I am trying to create a script for my sword that does damage to NPC’s (Drooling Zombie) and not to other players (player2). I have a script that does damage to everything but, I don’t know how to change it to do damage to only NPC’s, so I came here for help!
(Video for more context, trying to do damage to only NPC’s, I thought making teams for this thing, but I don’t think this is efficient)
local tool = script.Parent
local function onTouch(partOther)
local humanOther = partOther.Parent:FindFirstChild("Humanoid")
if not humanOther then return end
if humanOther.Parent == tool then return end
humanOther:TakeDamage(60)
end
local function slash()
local str = Instance.new('StringValue')
str.Name = "toolanim"
str.Value = "Slash"
str.Parent = tool
end
tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)
You can change the “Humanoid” in your script and in the zombie into “EnemyHumanoid” for example, so it only damages the NPCs with Humanoids named: “EnemyHumanoid”
local tool = script.Parent
local function onTouch(partOther)
local humanOther = partOther.Parent:FindFirstChild("Humanoid")
if not humanOther then return end
if humanOther.Parent == tool then return end
if game.Players:GetPlayerFromCharacter(partOther.Parent) then return end
humanOther:TakeDamage(60)
end
local function slash()
local str = Instance.new('StringValue')
str.Name = "toolanim"
str.Value = "Slash"
str.Parent = tool
end
tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)
A way to check if a character is a player’s character, check inside game.Players for either their name or use the function :GetPlayerFromCharacter().
So all you need to add there is just one line at the beginning to get the Players service local Players = game:GetService("Players")
…and a check inside the function can be either: if Players:GetPlayerFromCharacter(partOther.Parent) then return end
or if Players:FindFirstChild(partOther.Parent.Name) then return end as the name of the character is the same as the name of the player.
If you have any questions or found errors or improvements, don’t hesitate to ask me
I hope this helped