Problem Printing Tables

You can write your topic however you want, but you need to answer these questions:

  1. Q: What do you want to achieve? Keep it simple and clear!
    A: I want to print the table like a string including it’s content.

  2. Q: What is the issue? Include screenshots / videos if possible!
    A: I don’t know how much time passed but I clearly remember that I would print the table and its content just using the snippet below.

    local RandomTable = {["TableContent1"] = "Hello ", [2] = "World!"}
    print(RandomTable)
    

    But today it seems like that I can’t print the table like this. Also it prints some hexadecimal like this
    0x9044f509085b95a6 instead of the content of the table and ChatGPT also approved me at it is
    hexadecimal.

  3. Q: What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    A: I tried using tostring() but it did not solve my problem and similar questions was absolutely not similar.

Please describe if you know a solution for this!
I think I would not reply for 2 hours because I have got some tasks out.

1 Like

disable log mode
5821

3 Likes

Alternatively:

function Inspect(Object, Indent)
  local INDENT = "    "
  Indent = Indent or 0
  if type(Object) == "table" then
    local Inspection = "{"
    for i, v in pairs(Object) do
      if Inspection ~= "{" then
        Inspection = Inspection..","
      end
      Inspection = Inspection.."\n".. string.rep(INDENT, Indent) .. "["..Inspect(i, Indent + 1).."] = "..Inspect(v, Indent + 1)
    end
    return Inspection.."\n"..(string.rep(INDENT, Indent - 1)).."}"
  elseif type(Object) == "string" then
    return '"'..Object..'"'
  else
    return Object
  end
end
function print_t(Table)
  print(Inspect(Table, 1))
end

local RandomTable = {["TableContent1"] = "Hello ", [2] = "World!", [3] = {"Nested", "table"}}
print_t(RandomTable)

Output:

{
    [2] = "World!",
    ["TableContent1"] = "Hello ",
    [3] = {
        [1] = "Nested",
        [2] = "table"
    }
}
2 Likes

I tried relaunching Roblox Studio with log mode disabled but it did not work.
I will try the alternative now.

Oh I see, so those ‘hexadecimals’ is actually memory address of that table (which is the hexadecimal value you’re seeing)

To print the contents of the table, you need to iterate through it using a loop.

local RandomTable = {["TableContent1"] = "Hello ", [2] = "World!"}

for key, value in pairs(RandomTable) do
    print(key, value)
end

This will print:

TableContent1    Hello
2    World!

The pairs function is used to iterate over all elements in a table. It returns two values: the key and the value of the current element.

1 Like

Or just unpack the table

print(unpack({"Cat", "Dog", "Mouse"})
-- Cat Dog Mouse
1 Like

I tried unpack() but I lived some problems.

  1. Problem: Roblox Studio crashed instantly.
    Solution: I tried removing RunService.HeartBeat and used while loop instead because recently it was printing too much errors and I thought that caused the crash.

  2. Problem: Roblox Studio isn’t crashing but also not responsing inputs. I also see a Exhausted allowed execution time error. Hardly ever stopped the game.
    Solution: I tried increasing loops task.wait()

I think I will create a module script for printing tables and iterate through table using a loop.

Sorry guys I solved it now. Actually I am using Terrain:ReadVoxels() and the terrain is pretty huge in 3D space (1024, 128, 1024) so it prints lots of tables inside and inside all they contain materials. Also that is the reason why script exhausted allowed execution time.

I believe in I can handle the rest guys. Thanks for helps and snippets.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.