Help with looping dictionaries

Hello everyone, it’s been a while since I’ve had to make a scripting support topic but here we are. Today, I’m having trouble with cycle table references.

Example:

local dictionary = {
	a = {
		b = {
			
		}
	}
}

dictionary.a.b.a = dictionary.a
print(dictionary)

Running this code gives us an output that looks like this. :arrow_down:

   ▼  {
    ["a"] =  ▼  {
       ["b"] =  ▼  {
          ["a"] = "*** cycle table reference detected ***"
       }
    }
}

I want to be able to copy over the a and b and put it into b without it looping like this. (What I want below :arrow_down:)

    ["a"] =  ▼  {
       ["b"] =  ▼  {
          ["a"] =  ▼  {
             ["b"] = {}
          }
       }
    }
}

I’ve been trying for hours and any support is appreciated! Thanks, Joosan.

So you just want to clone the table?

local deep_copy
function deep_copy(t)
    local out = {}
    for key, value in pairs(t) do
        if type(value) == "table" then
            out[key] = deep_copy(value)
        else
            out[key] = value -- this won't clone Instances, they will be the same parts.
        end
    end
    return out
end

local a = {
    b = {
    
    }
}

a.b.a = deep_copy(a)
2 Likes

Hey, thanks for the response. This works perfectly. Thankyou!

1 Like