Add if statement to ChildAdded?

So I want to have I workspace.ChildAdded but I dont know how to add a if statement to it

_G.On = true
_G.Race = "Folder"

game:GetService("Workspace")["$raceColliders"].ChildAdded:Connect(function(ChildAdded)
    if ChildAdded.Name == _G.Name then
        print(ChildAdded.Name)
        ChildAdded.ChildAdded:Connect(function(ChildAdded2)
            if ChildAdded2.ClassName == "Part" then
                print(ChildAdded2.Name)
            end
        end)
    end
end)

I want to do something like

_G.On = true
_G.Name = "Folder"

if _G.On then
	game:GetService("Workspace").ChildAdded:Connect(function(ChildAdded)
		if ChildAdded.Name == _G.Name then
			print(ChildAdded.Name)
			ChildAdded.ChildAdded:Connect(function(ChildAdded2)
				if ChildAdded2.ClassName == "Part" then
					print(ChildAdded2.Name)
				end
			end)
		end
	end)
else
	HOW DO I TURN OF A CHILDADDED?
end

But how do I turn off the childadded?

RBXScriptSignal:Connect returns an RBXScriptConnection which you can use to disconnect the connection.

local connection
connection = game:GetService("Workspace").ChildAdded:Connect(function(child)
    -- do something here
end)

also you don’t need to use _G here :stuck_out_tongue:

Please don’t be acting unprofessional. If you have a problem with someone, please direct message that person or contact a moderator - the devforum scripting support area is not for nonsensical arguments and conversations. And for the record, your Roblox character appears to resemble a man, so don’t bring up “are you assuming my gender” unless you want to delve even further off topic unnecessarily. There’s no need for that.

Now getting on topic, it seems that you could just set G.on to true, and that should settle your problem. Or go along with what was earlier suggested through use of :Disconnect().

Hope this helps, and let us all know if there’s any questions.

2 Likes