An instance for each separate instance

What do you want to achieve?
I would like to create an NPC instance that will be created for example 10 times
and assigns it a parent as a folder, then it needs to add Clickdetector instances to it, which it will take from ReplicatedStorage and add to the NPC

The problem is that I don’t know how to create a Clickdetector for each individual NPC so that it doesn’t repeat itself

What solutions have you tried so far?

I tried to find information on the Internet, forum, etc. but I found nothing

1 Like

What exactly do you mean by creating a ClickDetector that doesn’t repeat itself?

When creating NPC instances, I want to add a separate clickdetector to each instance, but I don’t know how to do it and I simply create a pair of clickdetectors in one NPC.

something like this:

local NPC = game.workspace.NPC
local NPCInstaced = game.workspace.NPConMap.NPCInstaced

function createNPC()
	local clone = NPC:Clone()
	clone.Parent = NPCInstaced
    ClickDetector()
end

function ClickDetector()
	for i, v in pairs(NPCInstaced:GetChildren()) do
		if v.Name == "NPC" and v:FindFirstChild("Humanoid") then
			
			local clickDetectorNPC = game.ReplicatedStorage.Utility.ClickDetector:Clone()
			clickDetectorNPC.Parent = v
			
			clickDetectorNPC.MouseClick:Connect(function(playerClicked)
            --staff 
      end
   end
end

Ah, I see what you mean. I’ve implemented your code and it creates multiple ClickDetectors for each NPC. This is an easy fix. All you have to do is check whether an NPC already has a ClickDetector, and if it doesn’t, clone and add the ClickDetector into the NPC.

I have amended your function to include this change:

function ClickDetector()
	
	for i, v in pairs(NPCInstaced:GetChildren()) do
		if v.Name == "NPC" and v:FindFirstChild("Humanoid") and not v:FindFirstChild(game.ReplicatedStorage.Utility.ClickDetector.Name) then

			local clickDetectorNPC = game.ReplicatedStorage.Utility.ClickDetector:Clone()
			clickDetectorNPC.Parent = v
		end
	end
	
end

Furthermore, I would recommend that you keep clickDetectorNPC.MouseClick:Connect(function(playerClicked) in a separate script, for example, a script inside of the ClickDetector object that you are referring to.

1 Like

And another problem with this code is that you are trying to call a function before it is set. To fix this, just move the createNPC function to below click detector. I would also advise using

local clickDetector = Instance.new("ClickDetector")

instead of going to replicated storage and copying a click detector. If you made any changes to the click detector, you can just do

clickDetector.Parent = --whatever property that you want to change
2 Likes

It’s good practice to ensure that functions are defined before calling them. Although, Luau doesn’t matter in which order functions are placed, so long as they are defined.

1 Like

I tried creating a clickdetector from instance.new, but the same clickdetector is assigned to all NPCs, so clicking one causes all of them to click. that’s why I take clickdetector from replicatedstorage

Maybe making the names different will help? I would suggest making a table and then putting the click detectors in that and calling each click detector the name of the npc and then “click_detector” or something like that.

clickDetectorTable = {}

function ClickDetector()
	for i, v in pairs(NPCInstaced:GetChildren()) do
		if v.Name == "NPC" and v:FindFirstChild("Humanoid") then
			
			local clickDetectorNPC = game.ReplicatedStorage.Utility.ClickDetector:Clone()
			clickDetectorNPC.Parent = v
            clickDetectorTable[v.."clickDetector"] = clickDetectorNPC
			
			clickDetectorNPC.MouseClick:Connect(function(playerClicked)
            --staff 
      end
   end
end
print(clickDetectorTable)
1 Like

Thanks for the help, but I have one problem. How can I check in which NPC the clickdetector was clicked?

Assuming the ClickDetector is stored within the NPC model itself, and assuming that you have a script inside of the ClickDetector object.

Model → ClickDetector → Script

You can find the NPC that was clicked by retrieving the name of the model.
local npc = script.Parent.Parent.Name

The reason all the clickdetectors are connected, is because you’re connecting the same clickdetector everytime you make a new NPC. You’re also cloning it a bunch of times…

Is there any way to easily generate new clickdetectors or how you would do it?

I’d make the NPC a modulescript.

local npc = {}

npc.new = function()
  local self = {}
  
  local char = game.ReplicatedStorage.NPC:Clone()
  char.Parent = workspace

  char.HumanoidRootPart.ClickDetector.MouseClick:Connect(function(plr)
    print(plr.Name)
  end)
  
  return self
end

return npc