Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I view the contents of this table?

Asked by 6 years ago
Edited 6 years ago

I have a datastore that saves a table and when I print the table result when the player loads it prints this: table: 19918444 and it is not a table as far as I can see. Could someone help me with this? If you need the script I will post it. Also the for loop that is supposed to loop through the table doesnt work. Hope you guys can help me. Thanks!

Edit 1. saving part:

01game.Players.PlayerRemoving:Connect(function(plr)
02 
03    if plr.CanSave.Value then
04 
05        local specialKey = "User_"..plr.UserId
06        local plrName = plr.Name
07        local plrInfo = game.ServerStorage:FindFirstChild(plrName):WaitForChild("PlayerInfo")
08 
09        if plrInfo.OwnsTycoon.Value then
10 
11            local tycoonName = plrInfo.OwnedTycoon.Value
12            local tycoon = game.Workspace.Tycoons:FindFirstChild(tycoonName)
13            local moneyToSave = plrInfo.Money.Value
14            local buildLevelToSave = plrInfo.BuildLevel.Value
15            local purchasedItemsTable = getTableForDataStore(tycoon, plrInfo)
View all 27 lines...

loading part:

01script.Parent.Touched:Connect(function(hit)
02 
03    if script.Parent.CanCollide then
04        if hit.Parent:FindFirstChild("Humanoid") then
05            if hit.Parent.Humanoid.Health > 0 then
06 
07            local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
08            local plrName = plr.Name
09            local plrInfo = game.ServerStorage:FindFirstChild(plrName):WaitForChild("PlayerInfo")
10            local specialKey = "User_"..plr.UserId
11            local tycoon = script.Parent.Parent.Parent.Parent
12            local tycoonName = tycoon.Name
13            local mainDataTable
14            local serverTycoon = game.ServerStorage:FindFirstChild("Model"..tycoon.Color.Value.."Tycoon")
15 
View all 47 lines...

and the print at the end is what gives the output I set Please help!

0
post code pl0x0rz PoePoeCannon 519 — 6y

2 answers

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

To expand on iiiGodRoblox's answer

Arrays and dictionaries usually cannot be printed in a human readable format if just printed to console or tostringed.

tostring(table t) will return some weird string that we know is table: some hexadecimal number, but that doesn't really give a lot of information about the table, sure it's nice that we can print it but it's not really too useful to use right now.

For us to actually view and inspect an array or dictionary, we can either use some that returns the table as a string or __tostring metamethod.


How To Implement

If you're bored of reading and just want an easy solution, you can use

1local function DumpTable(t)
2    local str = {}
3    for Index, Value in pairs(t) do
4        table.insert(str, tostring(Value))
5    end
6    return "{" .. table.concat(str, ", ") .. "}"
7end

It's not necessarily the best solution, but it works and it's simple enough to understand at a glance.

If you're curious for more information, you can keep on reading.

There's an obvious problem with the code above, it doesn't support nested tables; let's say we have a table structured as such

01{
02    Hello = "World",
03    Programmed = {
04        "To"
05        "Work"
06    },
07    And = {
08        Not = "to",
09        Feel = true
10    }
11}

What our code above might output would be something along the lines of {World, table: 0xBAADF00D, table: 0xFFFFFF} Yeah, it prints it out but now we're back to our original problem, it's printing table: hex again.

What we can do is have some kind of recursive implementation that checks the type of Value and if it's a table, then dump that table

The code may fall something along the lines like

01local function DumpTable(t)
02    local str = {}
03    for Index, Value in pairs(t) do
04        if type(Value) == "table" then
05            table.insert(str, DumpTable(Value))
06        else
07            table.insert(str, tostring(Value))
08        end
09    end
10    return "{" .. table.concat(str, ", ") .. "}"
11end

If we inspect the code, we can see that it'll handle tables inside tables and tables inside those tables correctly.


Self referential tables

This should be enough for most people, but what if you're really eccentric and have something like

1local a = {}
2local b = {a}
3a[1] = a

Now, at first, this may seem to work, its just tables inside tables but now this presents another problem: self-referential tables

This is what our little function will do

1DumpTable(a)
2    -> __index(a, 1) = b
3    -> DumpTable(b)
4        -> __index(b, 1) = a
5        -> DumpTable(a)
6            -> __index(a, 1) = b
7            -> DumpTable(b)
8            ... forever

Now that's a problem, it just keeps on dumping a and b! This will go on infinitely and will cause a stack overflow which isn't good for both us and the computer.

What we can do to solve this is keep track of the tables that we already turned into strings, if it hasn't then do whatever we usually do, else just return tostring(t)

01local function DumpTable(t, Printed)
02    Printed = Printed or {}
03    if Printed[t] then
04        return tostring(t) --this is where we stop self-referential tables
05    end
06    Printed[t] = true
07    local str = {}
08    for Index, Value in pairs(t) do
09        if type(Value) == "table" then
10            table.insert(str, DumpTable(Value, Printed))
11        else
12            table.insert(str, tostring(Value))
13        end
14    end
15    return "{" .. table.concat(str, ", ") .. "}"
16end

Now, this is what our function will do with the table a and b problem:

01DumpTable(a, {})
02    -> __index(a, 1) = b
03    -> DumpTable(b, Printed)
04        ->__index(b, 1) = a
05        -> DumpTable(a, Printed)
06            -> a is in Printed, return tostring(a)
07        -> return "{" .. tostring(a) .. "}"
08    ->return "{{" .. tostring(a) .. "}}
09 
10DumpTable(a) == "{{" .. tostring(a) .. "}}"

This may seem not that useful but that's just because it isn't necessarily the best example; however, it clearly illustrates that, yes, it can handle self-referential tables while not having a stroke and dying.

Of course, this doesn't solve all our problems, it shows strings as String rather than "String", which isn't optimal and while yes it works with both arrays and dictionaries, dictionary support isn't exactly the best as it doesn't include the index. Using what you learned in this answer, try extending our code to work and solve these little problems.

If you want to use existing code to inspect tables, you can use Kikito's inspect.lua or my own table.FormatValue

0
Thank you so much! User#21908 42 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Well you could see the content of the table by using a for loop like the following

1local tab = {'a','b','c','d',...}
2for i,v in pairs(tab) do
3    print(i,v)
4end

Answer this question