Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

"Maximum event re-entrancy depth exceeded for Instance.ChildAdded"?

I'm trying to create a script which when you click a button, it creates a textLabel in a Frame, but it doesn't work.

To detect the click and make a value which is placed inside the frame.

local example1 = script.Parent:FindFirstChild("Example1")
local example2 = script.Parent:FindFirstChild("Example2")

local function createAList(name,price)
	local thing = Instance.new("StringValue",script.Parent.Parent.OrderFrame)
	thing.Name = name
	thing.Value = name.." : "..price
	local pos = Instance.new("NumberValue",thing)
	pos.Value = script.Parent.Parent.LocalLast.Value + 50
	pos.Name = "Hello"
	script.Parent.Parent.LocalLast.Value = script.Parent.Parent.LocalLast.Value + 50
end

example1.MouseButton1Click:connect(function()
	script.Parent.Parent.Total.Text = "Total: "..script.Parent.Parent.Total.Total.Value + example1.Price.Value
	script.Parent.Parent.Total.Total.Value = script.Parent.Parent.Total.Total.Value + example1.Price.Value
	createAList(example1.Text,example1.Price.Value)
end)
example2.MouseButton1Click:connect(function()
	script.Parent.Parent.Total.Text = "Total: "..script.Parent.Parent.Total.Total.Value + example2.Price.Value
	script.Parent.Parent.Total.Total.Value = script.Parent.Parent.Total.Total.Value + example2.Price.Value
	createAList(example2.Text,example2.Price.Value)
end)

Checks every time a child is added.

script.Parent.ChildAdded:connect(function()
	for i,v in pairs(script.Parent:GetChildren()) do
		if v.ClassName == "StringValue" then
			local order = Instance.new("TextLabel",script.Parent)
			order.Text = v.Value
			wait()
			order.Position = UDim2.new(0,0,0,v:FindFirstChild("Hello").Value)
		end
	end
end)

But when I try this, I get the error

17:09:22.353 - Maximum event re-entrancy depth exceeded for Instance.ChildAdded
17:09:22.353 - While entering function defined in script 'Players.Player1.PlayerGui.OrderGuiMobile.Screen.OrderFrame.Script', line 1

Any help will be appreciated!

1 answer

✓ Accepted answer

From a brief lookover of your script, the problem is that you are adding a child in the event handler for the 'ChildAdded' event, so whenever a child is added, you create a new "TextLabel" instance under the same parent, which also counts as a child and so calls the event handler again (and again, and again), until a certain re-entrancy depth is reached.

script.Parent.ChildAdded:connect(function() -- This function runs when a new child is added to 'script.Parent'
	    for i,v in pairs(script.Parent:GetChildren()) do
	        if v.ClassName == "StringValue" then
	            local order = Instance.new("TextLabel",script.Parent) -- This creates a new child under 'script.Parent'
	            order.Text = v.Value
	            wait()
	            order.Position = UDim2.new(0,0,0,v:FindFirstChild("Hello").Value)
	        end
	    end
	end)

A quick test indicates that the maximum re-entrancy depth is about 6. (That is, an event handler can call itself 6 times before it is stopped.)

local part = Instance.new("Part", workspace)

local counter = 0

part.ChildAdded:connect(function()
	local newPart = Instance.new("Part", part)
	newPart.Name = tostring(counter)
	counter = counter + 1
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.