Plugin runs a loop but when a new part is inserted it doesn't add an attribute

The title is quite self explanatory. Basically, my plugin has a for loop that searches workspace’s children and add and attribute to them however, when I insert a new part an attribute isn’t added. Here’s the script chunk that adds an attribute to every part and yes, this is a module script:

function shapemodule:SetUp()
	for i,v in pairs(workspace:GetChildren()) do
		if v:IsA("Part") or v:IsA("UnionOperation") then
			     v:SetAttribute("CustomPart1", false)
		   	v:SetAttribute("Triangle", false)
		             	v:SetAttribute("Egg", false)
		       	v:SetAttribute("Ring", false)
		            	v:SetAttribute("Arrow", false)
			    v:SetAttribute("CustomPart2", false)
		  	v:SetAttribute("CustomPart3", false)
		 	v:SetAttribute("Diamond", false)
		     	v:SetAttribute("CustomPart4", false)
		end

Is this the full code? if so you forgot to add 1 end at the end

From what I can tell with the code snippet you provided. This function will only run once called, not when a new part is added to the Workspace. I suggest you hook your function with a ChildAdded or DescendantAdded event - so it can run every time a new part is added.

To add onto this, if you want it to run every time a new part is added, make sure to do an if statement to check if said attribute already exists on a part, as it’ll scan through every single part readding the attribute if an if statement isn’t there.

1 Like

Sorry for the late reply but this unfortunately didn’t work. Here’s my script:

function shapemodule:SetUp()
	game.Workspace.ChildAdded:Connect(function(child)
		if child:IsA("Part") or child:IsA("UnionOperation") then
			child:SetAttribute("CustomPart1", false)
			child:SetAttribute("Triangle", false)
			child:SetAttribute("Egg", false)
			child:SetAttribute("Ring", false)
			child:SetAttribute("Arrow", false)
			child:SetAttribute("CustomPart2", false)
			child:SetAttribute("CustomPart3", false)
			child:SetAttribute("Diamond", false)
			child:SetAttribute("CustomPart4", false)
		end
		end)

When you write it like that, the ChildAdded event will be connected every time the function SetUp() is called, which isn’t a good practice since if you call the function more than once, for each part added the attributes will be added twice.

I’d suggest an approach like this:

function shapemodule:SetUp()
	for i,v in pairs(workspace:GetChildren()) do
		if v:IsA("Part") or v:IsA("UnionOperation") and not v:GetAttribute("CustomPart1") then
			v:SetAttribute("CustomPart1", false)
		   	v:SetAttribute("Triangle", false)
            v:SetAttribute("Egg", false)
		    v:SetAttribute("Ring", false)
		    v:SetAttribute("Arrow", false)
			v:SetAttribute("CustomPart2", false)
		  	v:SetAttribute("CustomPart3", false)
		 	v:SetAttribute("Diamond", false)
		    v:SetAttribute("CustomPart4", false)
      end
	end
end

game.Workspace.ChildAdded:Connect(SetUp)

shapemodule:SetUp()

As you can see I’ve made a few changes to your code; firstly I connected the SetUp function to a ChildAdded event, so for each new part in the workspace it’ll run the loop once more.
game.Workspace.ChildAdded:Connect(SetUp)

Secondly, I’ve also called the function individually, so it already sets up existing parts when the plugin boots up.
shapemodule:SetUp()

Lastly I’ve added a check to make sure that, once we loop through every part in the Workspace, we don’t repeat the process for parts we’ve already implemented attributes on.
and not v:GetAttribute("CustomPart1")

Okay, so I’ve did this since I couldn’t exactly replicate what you wanted me to do since it’d error:

function SetUp()
	for i,v in pairs(workspace:GetChildren()) do
		if v:IsA("Part") or v:IsA("UnionOperation") then
			v:SetAttribute("CustomPart1", false)
			v:SetAttribute("Triangle", false)
			v:SetAttribute("Egg", false)
			v:SetAttribute("Ring", false)
			v:SetAttribute("Arrow", false)
			:SetAttribute("CustomPart2", false)
			v:SetAttribute("CustomPart3", false)
			v:SetAttribute("Diamond", false)
			v:SetAttribute("CustomPart4", false)
		end
		end
		end
game.Workspace.ChildAdded(SetUp)
SetUp()

But after that, when I set an attribute to true, it didn’t do anything.

Hey, I do still need help with this, it’d be pleasing if you’d answer me.