How can i make my touched part do something when a NPC touches it?

so I have an NPC made and I have put in a bool value the values name is IsNPC and it is false in players and true in NPCs and I want to make 1 part do something when a player touches and something when an NPC touches, I have made 2 scripts and it does what it needs to do when a player touches and in another script, I have put in some code but I don’t know how to separate NPCs and players when they touch the part here’s the code for when the NPC touches (it does not work)

local part = script.Parent


part.Touched:Connect(function(hit)
local parent = hit.Parent
local NPC = workspace.Walk

	if NPC then
		print("pls work pls i have been trying for so long")

	end
end)

so I want to know, how can I make it only work when the NPC touches it and do nothing when a player touches?

Well you can add an element into the NPC (such as a BoolValue, a Folder, or anything that doesn’t interferes with the NPC directly), give it a name and add an if statement under the .Touched event. Something like this:

local part = script.Parent
part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("NPC") then
       print("NPC found.")
    end
end)
4 Likes

i have add a bool value and named it IsNPC and made it true players have it too but it is false for them will that work?

Just create the value into the NPCs if you only want the .Touched event to detect NPCs. Then check if in the hit.Parent something named “NPC” is found, then write your code.

1 Like