For loop detection, including things added after said loop is run (help)

  1. What do you want to achieve?
    I want to run a for loop to detect when any click detectors are activated, including click detectors added after the loop is run.

Is there a way to do this without lagging out my game?

  1. What is the issue?
    It doesn’t detect the click detectors added after the loop is run (understandably).

  2. What solutions have you tried so far?
    I tried running the loop every time a child was added to workspace, if the child was a click detector. I’m not sure why, but this broke it. Terribly. I tried googling how to achieve this, but nothing.

for i, Pickable in pairs(workspace:GetDescendants()) do
	if Pickable.Name == "Pickable" then
		Pickable.MouseClick:Connect(function()
			print("Okay")
		end)
	end
end
1 Like

I recommend collection services. You can tag the click detectors and detect when only the click detectors are added without needing to loop through the entire workspace and cause lag.

1 Like
local function ConnectClickDetector(Pickable)
Pickable.MouseClick:Connect(function()
			print("Okay")
		end)
end

for i, Pickable in pairs(workspace:GetDescendants()) do
	if Pickable.Name == "Pickable" then
		ConnectClickDetector(Pickable)
	end
end

workspace.DescendantAdded:Connect(function(obj)
if obj:IsA'ClickDetector' and obj.Name == 'Pickable' then
ConnectClickDetector(obj)
end
end)
1 Like

wait ill edit script first wait a moment

Besides using the ChildAdded event for the workspace, you could use attributes to detect whenever a ClickDetector is added to the workspace.

How I would do it is I’d add a boolean attribute to the workspace, then set it to false. Then in whatever script that controls when a new ClickDetector is added to the workspace, I would also set the workspace’s boolean attribute that we added earlier to “true” by doing workspace:SetAttribute(“NameOfAttribute, true”) after you have parented the new ClickDetector to the workspace.

Then I would use the GetAttributeChangedSignal() function to detect when the Attribute’s value is changed to true, and then put the for loop function you had above inside this function.

I don’t really see any reason why ChildAdded wouldn’t work, so I’m guessing you implemented incorrectly somehow.

1 Like

The best way to do this would be to use the Changed event on the ClickDetector. This event will be triggered whenever the ClickDetector is added, removed, or modified. You can then use this event to update the loop that you have to include the new ClickDetector.

workspace.DescendantAdded:Connect(function(descendant)
	if descendant:IsA("ClickDetector") then
		descendant.MouseClick:Connect(function()
			print("Okay")
		end)
	end
end)
1 Like

I have two questions.

  1. If I have a tagged instance, and I clone it, will the clone also have the tag? This is really important.

  2. So this is basically a more efficient method of looping through workspace, yes?

1 Like

Yes from my experience.

No, it does not loop through workspace it does something different. It would be incorrect and misleading to say it loops through workspace.

It instead only loops through the instances you want that are tagged much different than looping through the entire workspace.

Typically more efficient as tagged instances are usually less than the total instances in workspace.

1 Like

That’s what I meant, thank you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.