Advertisement
Vzurxy

tableToString

Dec 30th, 2019
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. roblox_typeof = typeof or type;
  2. roblox_typeof_new = {
  3.     BrickColor = true,
  4.     Color3 = true,
  5.     Vector2 = true,
  6.     Vector3 = true,
  7.     CFrame = true,
  8.     Region3 = true,
  9.     UDim2 = true
  10. };
  11.  
  12. push_and_clone = function (tbl, val)
  13.     local clone = {};
  14.    
  15.     for k, v in next, tbl do
  16.         clone[k] = v;
  17.     end;
  18.    
  19.     clone [#clone + 1] = val;
  20.     return clone;
  21. end;
  22.  
  23. table_to_string = function (tbl, index, path)
  24.     local index = index or 1;
  25.     local path = path or {};
  26.     local output = (getmetatable (tbl) == nil and '' or '[METATABLE]') .. '{\n';
  27.    
  28.     for i, v in next, path do
  29.         if (v == tbl and i ~= #path) then
  30.             return '[Circular Reference]';
  31.         end;
  32.     end;
  33.    
  34.     for k, v in next, tbl do
  35.         output = output .. string.rep ('    ', index) .. '[';
  36.        
  37.         if (roblox_typeof (k) == 'table') then
  38.             output = output .. table_to_string (k, 1, push_and_clone (path, k));
  39.         elseif (roblox_typeof (k) == 'string') then
  40.             output = output .. '"' .. k .. '"';
  41.         elseif (roblox_typeof_new [roblox_typeof (k)]) then
  42.             output = output .. roblox_typeof (k) .. '.new (' .. tostring(v) .. ')'
  43.         else
  44.             output = output .. tostring (k);
  45.         end;
  46.        
  47.         output = output .. '] = ';
  48.        
  49.         if (roblox_typeof (v) == 'table') then
  50.             output = output .. table_to_string (v, index + 1, push_and_clone (path, v));
  51.         elseif (roblox_typeof (v) == 'string') then
  52.             output = output .. '"' .. v .. '"';
  53.         elseif (roblox_typeof_new [roblox_typeof (v)]) then
  54.             output = output .. roblox_typeof (v) .. '.new (' .. tostring(v) .. ')'
  55.         else
  56.             output = output .. tostring (v);
  57.         end;
  58.        
  59.         output = output .. '\n';
  60.     end;
  61.    
  62.     return output .. string.rep ('    ', math.max (index - 1, 0)) .. '};';
  63. end;
  64.  
  65. getgenv().tableToString = table_to_string;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement