How can I enable a damage indicator only when attacking NPCs?

Hello Developers! I need help with a script which shows the amount of damage when attacking an NPC. However although it dosent do damage to players it still shows numbers on top of the players head which means it says damage is being done even though their health bar doesn’t go down on either screen. So how can I make it that it only shows the numbers of damage when you are attacking NPC’s and not for players, because in my game you cant attack players. Here is my script which triggers the damage indicator:

if IsServer then
		if target:IsA("Humanoid") and dealer:IsA("Player") and dealer.Character then
			local dealerHumanoid = dealer.Character:FindFirstChildOfClass("Humanoid")
			local targetPlayer = Players:GetPlayerFromCharacter(target.Parent)
			if dealerHumanoid and target ~= dealerHumanoid and targetPlayer and game.Players:GetPlayerFromCharacter(dealer.Parent) then
				-- Trigger the damage indicator
				WeaponData:FireClient(targetPlayer, "HitByOtherPlayer", dealer.Character.HumanoidRootPart.CFrame.Position)
			end
		end

Please modify it so it only shows when attacking NPC’s and not players!

2 Likes

Any help will be highly appreciated!

What kind of damage system do you use? Do you have like a punch localscript, or tools or anything?

I have the script right there it activates when the bullet has touched a object containing a humanoid. I want it to check if the object is a NPC or player.

This is in the script which does the damage

To tell if the character is an NPC you can use a this function

game:GetService("Players"):GetPlayerFromCharacter()

If it returns nil then it is not a player.

You can rename the Humanoid of the Player to “Usernoid” or rename the humanoid of your NPCs.
Edit:
You can also do it like this

local hit = -- touched part
if game:GetService("Players"):FindFirstChild(hit.Name) then
print("is player")
else
print("is no player")
end

How can I implement this into my script?

Ok thankyou but let me see what he says.

Try this

if IsServer then
		if target:IsA("Humanoid") and dealer:IsA("Player") and dealer.Character then
			local dealerHumanoid = dealer.Character:FindFirstChildOfClass("Humanoid")
			local targetPlayer = Players:GetPlayerFromCharacter(target.Parent)
			if dealerHumanoid and target ~= dealerHumanoid and targetPlayer and game.Players:GetPlayerFromCharacter(dealer.Parent) then
				-- Trigger the damage indicator
if game:GetService("Players"):FindFirstChild(target.Parent.Name) then
WeaponData:FireClient(targetPlayer, "HitByOtherPlayer", dealer.Character.HumanoidRootPart.CFrame.Position)
else
print("is no player")
end
			end
		end

The function expects the Character object but you gave a string, so it would return nil either way.

If you have the humanoid set as the variable target then do this:

local target = -- the humanoid
local player = game:GetService("Players"):GetPlayerFromCharacter(target.Parent)
if player then
    -- code for the player
else
    -- code for the NPC
end

Does that mean his new method also wont work?

@DindinYT37 has gave a good way! Thankyou for trying to help anyway.

You can add a value inside the npc and name it something like npc and give it a true and then in the script check if targetYouHit:FindFirstChild(“IsNpc”) ~= nil then

I think it is better to use @DindinYT37 way, but thanks for that way too!

Right now I just want to see if @DindinYT37 way works.

1 Like

Does it load the damage indicator if you hit an NPC?

I am trying @DindinYT37 way now. Hope it works!

@DindinYT37 did not work is there a way to make it work? Can anyone make the correct edit?

Either use @iFlameyz or my code