How do i make attack only hit npcs

How do i make the attack only hit npcs? Someone please help me

Wave.Touched:Connect(function(hit)
			if hit:IsA("MeshPart") or hit:IsA("Part") then
				if not hit:IsDescendantOf(game.Workspace.Mobs.NPC) then
					local EHum = hit.Parent:FindFirstChild("npchum")
					
					if EHum then
						if EHum:FindFirstChild(EHum) then
							local debounce = Instance.new("BoolValue", EHum)
							debounce.Name = "Debounce"
							Debris:AddItem(debounce,.75)
							
							EHum:TakeDamage(Damage)
						end
					end
				end
			end
		end)

Thank you

2 Likes

You could try something like this.

local Debris = game:GetService("Debris")
local Players = game:GetService("Players")

local function FindHumanoid(hit)
	local part = hit.Parent
	
	if part:FindFirstChild("Humanoid") then
		return part:FindFirstChild("Humanoid")
	elseif part.Parent:FindFirstChild("Humanoid") then
		return part.Parent:FindFirstChild("Humanoid")
	end
end

Wave.Touched:Connect(function(hit)
	local human = FindHumanoid(hit)
	if human then
		if not Players:GetPlayerFromCharacter(human.Parent) then -- Make sure it's not a player
			if human:FindFirstChild("IsNPC") and not human:FindFirstChild("Debounce") then -- You could insert a boolvalue inside the npcs humanoid for double check
				local debounce = Instance.new("BoolValue", human)
				debounce.Name = "Debounce"
				Debris:AddItem(debounce, .75)
				human:TakeDamage(10)
			end
		end
	end
end)

what should the boolvalue name be and what should the value be set to?

Insert a BoolValue inside your NPC’s humanoid named “IsNPC” and set its value to true.

weird, it still doesn’t do any damage?

you can name the humanoids differently

i named the humanoid differentlyScreen Shot 2021-03-13 at 9.18.26 AM

local Debris = game:GetService("Debris")
local Players = game:GetService("Players")

local function FindHumanoid(hit)
	local part = hit.Parent
	
	if part:FindFirstChild("npchum") then
		return part:FindFirstChild("npchum")
	elseif part.Parent:FindFirstChild("npchum") then
		return part.Parent:FindFirstChild("npchum")
	end
end

Wave.Touched:Connect(function(hit)
	local human = FindHumanoid(hit)
	if human then
		if not Players:GetPlayerFromCharacter(human.Parent) then -- Make sure it's not a player
			if human:FindFirstChild("IsNPC") and not human:FindFirstChild("Debounce") then -- You could insert a boolvalue inside the npcs humanoid for double check
				local debounce = Instance.new("BoolValue", human)
				debounce.Name = "Debounce"
				Debris:AddItem(debounce, .75)
				human:TakeDamage(10)
			end
		end
	end
end)

The script should look like this now then

That’s odd it should be getting the character first and finding the player from that

oh i used player in a fireclient event

local rp = game:GetService("ReplicatedStorage")
local detroit = rp.Events:WaitForChild("Skill1")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local cam = rp.Events:WaitForChild("Camm")

local Damage = 20

local Animation = script:WaitForChild("Animations")
local Meshes = script:WaitForChild("Meshes")

detroit.OnServerEvent:Connect(function(player)
	local Character = player.Character
	local humanoid = Character:WaitForChild("Humanoid")
	local HumRP = Character:WaitForChild("HumanoidRootPart")
	
	local smash = humanoid:LoadAnimation(Animation.Animated)
	smash:Play()
	
	local Effects = Instance.new("Folder", Character)
	Effects.Name = "Effects"
	Debris:AddItem(Effects,3)
	
	smash.KeyframeReached:Connect(function(PunchFrame)
		local Wave = Meshes:WaitForChild("Wave"):Clone()
		Wave.CFrame = HumRP.CFrame * CFrame.new(0,0,-3)
		Wave.Parent = Effects
		Debris:AddItem(Wave,.75)
		
		spawn(function()
			cam:FireClient(player)
		end)
		
		local sound = Instance.new("Sound",Character)
		sound.SoundId = "rbxassetid://464615492"
		sound:Play()
		Debris:AddItem(sound,.75)
		
		local goal = {}
		goal.Size = Wave.Size + Vector3.new(3,3,4)
		local info = TweenInfo.new(.25)
		local tween = TweenService:Create(Wave,info,goal)
		tween:Play()
		
		local goal2 = {}
		goal2.CFrame = Wave.CFrame * CFrame.new(0,0,-75)  
		goal2.Transparency = Wave.Transparency + (1 - Wave.Transparency)
		local info2 = TweenInfo.new(.75,Enum.EasingStyle.Linear)
		local tween2 = TweenService:Create(Wave,info2,goal2)
		tween2:Play()
		
		local function FindHumanoid(hit)
			local part = hit.Parent

			if part:FindFirstChild("npchum") then
				return part:FindFirstChild("npchum")
			elseif part.Parent:FindFirstChild("npchum") then
				return part.Parent:FindFirstChild("npchum")
			end
		end

		Wave.Touched:Connect(function(hit)
			local human = FindHumanoid(hit)
			if human then
				if not player:GetPlayerFromCharacter(human.Parent) then -- Make sure it's not a player
					if human:FindFirstChild("IsNPC") and not human:FindFirstChild("Debounce") then -- You could insert a boolvalue inside the npcs humanoid for double check
						local debounce = Instance.new("BoolValue", human)
						debounce.Name = "Debounce"
						Debris:AddItem(debounce, .75)
						human:TakeDamage(10)
					end
				end
			end
		end)

		for i=1, 25 do
			local Rocks = Meshes:WaitForChild("Rocks"):Clone()
			Rocks.CFrame = Wave.CFrame * CFrame.new(0,-3,0)
			Rocks.Parent = Effects
			Debris:AddItem(Rocks,3)
			
			local rGoal = {}
			rGoal.Transparency = Rocks.Transparency + (1- Rocks.Transparency)
			local rInfo = TweenInfo.new(3)
			local rTween = TweenService:Create(Rocks,rInfo,rGoal)
			rTween:Play()
			
			wait()
		end
		
	end)
	detroit:FireClient(player)
end)

ohh ok I was kind of confused, but does the script work?

the damage doesn’t work

local player = players:GetPlayerFromCharacter(character)

if player then return end

where should i put that part? and it’s saying that character is an unknown global

Wait so the wave object is a weapon that damages npcs?

yes?

1 Like

Oh ok thanks I just didn’t want to get it backwards

1 Like

Before the damage code for your script runs.

First of all let’s find a character

if hitpart.Parent:FindFirstChild("Humanoid") then
local Character = hitpart.Parent
local player = game.Players:GetPlayerFromCharacter(Character) 
if player then return end
-- your code
end
Wave.Touched:Connect(function(hit)
		if hit:IsA("BasePart") then
			if not hit:IsDescendantOf(game.Workspace.Mobs.NPC) then
				local EHum = hit.Parent:FindFirstChild("npchum")
				if EHum then
					local debounce = Instance.new("BoolValue", EHum)
					debounce.Name = "Debounce"
					Debris:AddItem(debounce,.75)
					
					EHum:TakeDamage(Damage)
				end
			end
		end
end)

Does This work?