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

Why does this give me an error, because it executes it too many times?

local db = 1
local max = 50

local function SpawnThem()
	for i, v in game.Workspace.Map:GetChildren() do
		for n = 1, max do
			local s = script.Coin:Clone()
			s.Parent = game.Workspace.Coins[i]
			local tel = v.Baseplate
			s.Position = tel.Position - Vector3.new(math.random(0,tel.Size.X) - tel.Size.X / 2,-5,math.random(0,tel.Size.Z) - tel.Size.Z / 2)
			s.Rotation = Vector3.new(0, math.random(0, 360), 0)
		end
	end
	wait(0.1)
	db = 1
end

SpawnThem()

local coins1 = game.Workspace.Coins["1"]
local coins2 = game.Workspace.Coins["2"]
local coins3 = game.Workspace.Coins["3"]
local coins4 = game.Workspace.Coins["4"]
local coins5 = game.Workspace.Coins["5"]

coins1.ChildRemoved:Connect(function()
	if #coins1:GetChildren() < max / 5 then
		for i, v in coins1:GetChildren() do
			v:Destroy()
		end
		if db == 1 then
			db = 0
			SpawnThem()
		end
	end
end)
coins2.ChildRemoved:Connect(function()
	if #coins2:GetChildren() < max / 5 then
		if db == 1 then
			db = 0
			for i, v in coins2:GetChildren() do
				v:Destroy()
			end
			SpawnThem()
		end
	end
end)
coins3.ChildRemoved:Connect(function()
	if #coins3:GetChildren() < max / 5 then
		if db == 1 then
			db = 0
			for i, v in coins3:GetChildren() do
				v:Destroy()
			end
			SpawnThem()
		end
	end
end)
coins4.ChildRemoved:Connect(function()
	if #coins4:GetChildren() < max / 5 then
		if db == 1 then
			db = 0
			for i, v in coins4:GetChildren() do
				v:Destroy()
			end
			SpawnThem()
		end
	end
end)
coins5.ChildRemoved:Connect(function()
	if #coins5:GetChildren() < max / 5 then
		if db == 1 then
			db = 0
			for i, v in coins5:GetChildren() do
				v:Destroy()
			end
			SpawnThem()
		end
	end
end)

ErrorMessage: Maximum event re-entrancy depth exceeded for Instance.ChildRemoved (x4)

1 answer

✓ Accepted answer

so first you remove items it fires

coins5.ChildRemoved

then while inside of the function you remove again which causes a loop then respawns again when connecting with ChildRemoved i usually think of it as creating a new script running that function then stops so that means your running a huge amount of loops

2 comments

0

Idk what you said but i added a debounce and it fixed everything but you can have an accept anyway

0

basically your destroying in a ChildRemoved function

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