Bullet damages players, but not NPCs

Hello, as the title states, I want a bullet to damage NPCs and players, but only damages players, despite NPCs humanoid having the same name. All of this is done on the client, but send the damage to the server to damage. Here is the client code:

local ts = game:GetService("TweenService")
game.Workspace.Events.Gun.OnClientEvent:Connect(function(handle) -- handle instance of the gun
	if game.Players.LocalPlayer.Name == handle.Parent.Parent.Name then
	local part = game.ReplicatedStorage.BlackHole:Clone()
	game:GetService("Debris"):AddItem(part,20)
		part.Size = Vector3.new(1,1,1)
		part.Name = "Handle" -- in case things interfered because of name
	part.Parent = game.Workspace
	local v = Instance.new("BodyVelocity")
	v.Parent = part
	v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	v.Velocity = game.Players.LocalPlayer:GetMouse().Hit.LookVector * 120
	part.Position = handle.Position
	part.Anchored = false
		coroutine.wrap(function()
			ts:Create(part,TweenInfo.new(0.6,Enum.EasingStyle.Quint,Enum.EasingDirection.InOut,0,false,0),{Size = Vector3.new(15,15,15)}):Play()
		part.Touched:Connect(function(h)
				local attackdebounce = false
				if h.Parent then
					local hum = h.Parent:FindFirstChildOfClass("Humanoid")
					if hum and hum.Health > 0 then	
						if hum.Parent.Name ~= handle.Parent.Parent.Name then
							if dbb == false then
								dbb = true
								game.Workspace.Events.Gun:FireServer(hum)
								wait(0.5)
								dbb = false
						end
					end						
				end
			end	
		end)
		end)
	end
end)
3 Likes

well theres two ways to do it,

  1. make a wall(easy)
  2. use TakeDamage to reduce the dmg to 0 or Humanoid.Health and make the health super high

PS: maybe forcefield and like make it invisible

1 Like

It seems to be not reaching somewhere, I recommend adding print statements to see where the code is stopping when attempting to damage an npc

1 Like

It doesnt detect touched on the NPC though, I already use take damage on the server when i fire the humanoid instance to the server. as for the wall, the bullet size increase after being fired, and is large enough to hit no matter what. It hits player models, but not NPCs

1 Like

You will need to show the onServerEvent code also i found out that you are creating a listener (Part.Touched) but not deleting it . That could lag your game

1 Like

Is your NPC moving or not? Could it be that?

Its not much, it just damages it on the server.

game.Workspace.Events.Nuke.OnServerEvent:Connect(function(plr,hum)
	hum:TakeDamage(hum.Maxhealth)
end)

Just create a folder of all the NPC’s load all children to a list and check if the part touched is the child of an NPC in the folder…

Its a game where the NPCs dont move, but launch attacks at you, so it cant be that.

Just remove humanoid if the NPC’s don’t need to move…

well you can use LocalPlayer, it can see if its a player or NPC

You still need to damge them though.

if you remove the humanoid npc gonna have no cloths on

Wait this doesn’t make sense

You said you didn’t need the NPC to be damaged just remove all NPC humanoid

I said ‘I want a bullet to damage NPCs and players’ plus the clothes would disappear, and some npcs rely on apply description.

well you can sort thing out as LocalPlayer and Npcs

There needs to be some debugging, try this

local ts = game:GetService("TweenService")
game.Workspace.Events.Gun.OnClientEvent:Connect(function(handle) -- handle instance of the gun
	if game.Players.LocalPlayer.Name ~= handle.Parent.Parent.Name then return end
	local part = game.ReplicatedStorage.BlackHole:Clone()
	game:GetService("Debris"):AddItem(part,20)
	part.Size = Vector3.new(1,1,1)
	part.Name = "Handle" -- in case things interfered because of name
	part.Parent = workspace
	local v = Instance.new("BodyVelocity")
	v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	v.Velocity = game.Players.LocalPlayer:GetMouse().Hit.LookVector * 120
	v.Parent = part
	part.Position = handle.Position
	part.Anchored = false
	coroutine.wrap(function()
		ts:Create(part,TweenInfo.new(0.6,Enum.EasingStyle.Quint,Enum.EasingDirection.InOut,0,false,0),{Size = Vector3.new(15,15,15)}):Play()
		part.Touched:Connect(function(h)
			local attackdebounce = false
			local char = h.Parent
			local hum = char:FindFirstChildOfClass("Humanoid")
			print(hum, hum.Health, char.Name, dbb)
			if not hum or hum.Health <= 0 or char.Name == handle.Parent.Parent.Name or dbb then	return end
			dbb = true
			game.Workspace.Events.Gun:FireServer(hum)
			wait(0.5)
			dbb = false
		end)
	end)()
end)

And see what prints out when you hit an npc

I got a ‘attempt to index nil with Health’ on the line with print(hum, hum.Health, char.Name, dbb)

It isn’t finding a humanoid then, for now, change the print to this

print(hum, char.Name, dbb)

might be because the npc doesnt have a health script,im not a good scripter so maybe check that