I’m making a script that “reloads” a folder meaning it loops through every object in the default folder and if it isnt in the folder it adds it in. It all works but the functions gets called about 5000 times and really slows down my computer
local function checkChildren(object)
for index, child in script.Parent.DefaultObjects["Folder"]:GetChildren() do
local identicalChild = object:FindFirstChild(child.Name)
if not(identicalChild) then
child:Clone().Parent = object
end
local childrenCount = 0
for index, c in pairs(child:GetChildren()) do
childrenCount += 1
end
if childrenCount > 0 then
for index, child in pairs(child:GetChildren()) do
checkChildren(child)
end
end
end
end
local function reloadFolder()
local folder = game.ReplicatedStorage:FindFirstChild("Chatterblox Dialogue") or Instance.new("Folder")
folder.Name = "Folder"
folder.Parent = game.ReplicatedStorage
checkChildren(folder)
end
I am pretty sure it happens because it will infinitely call the checkChildren() function because you pre-create an identicalChild if one does not exist, which results in child:GetChildren() adding one more to it & continuing with the cycle infinitely until the script crashes
Hello, The function checkChildren seems to be recursively calling itself for every child object, which can indeed lead to a large number of calls if the hierarchy is deep or contains many objects. This can cause significant performance issues.
local function checkChildren(object)
for _, child in ipairs(script.Parent.DefaultObjects["Folder"]:GetDescendants()) do
local identicalChild = object:FindFirstChild(child.Name)
if not identicalChild then
child:Clone().Parent = object
end
end
end
local function reloadFolder()
local folder = game.ReplicatedStorage:FindFirstChild("Chatterblox Dialogue") or Instance.new("Folder")
folder.Name = "Folder"
folder.Parent = game.ReplicatedStorage
checkChildren(folder)
end
Try using this script and let me know If It helped you!