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

Converting table to string returns nil, help?

Asked by 4 years ago
while true do
    local date_table = os.date("!*t")
    print(date_table)
    function tableToString(t)
    local s = ""
    for i = 1,#t do
        s = s..tostring(t)
        if i ~= #t then
            s = s..","
        end
    end
    return s
    end
    local converted = tableToString(date_table)
    print(converted)
    script.Parent.TextLabel.Text = ""..converted
    wait (1)
end

While converting my table to a string it returned nothing why is it doing this No errors are shown

2 answers

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

OS.DATE() and tableToString() functions

Each item in os.date("!*t") has a key and a value. You can get values inside the table such as the time by saying (os.date variable).key:

currentTime = os.date("!*t")
print(currentTime.hour) --prints the current hour (hour is the key, whatever it prints is the value)
print(currentTime.min) --prints the current minute  (minute is the key, whatever it prints is the value)
print(currentTime.day) -- prints the current day (day is the key, whatever it prints is the value)
print(currentTime.month) --prints the current month (month is the key, whatever it prints is the value)

See later in the post for a complete list of keys that the table os.date("!*t") contains

Anyway, your code should be:

function tableToString(tableToConvert)
    local resultString = ""

    for key, value in pairs(tableToConvert) do
        --prints the key and value for each item in the table,
        --then starts a new line with the '\n' character.
        --if you don't want a new line, get rid of  .. "\n"
            resultString = resultString .. tostring(key) .. ": " .. tostring(value) .. "\n"
    end

    return resultString
end


while true do
    local date_table = os.date("!*t")
    print(date_table) --this prints out the table itself, not the contents of the table


    local resultString = tableToString(date_table)
    print(resultString)

    script.Parent.TextLabel.Text = resultString
    wait (1)
end

Output should look similar to this:

table: 0xee2ed6d66812dd73 <-- this is the table itself printed out when you say print(date_table)

--This is resultString:
hour: 22
min: 16
wday: 7
day: 30
month: 11
year: 2019
sec: 6
yday: 334
isdst: false

hour, min, wday, day, month, year, sec,yday, isdst are all the keys inside the table os.date("!*t")


UNPACK()

unrelated but, this is unpack() that i was talking about earlier:

myTable = {"this is a value", "this is another value", 123, 4354353454}
print(unpack(myTable))

output:

this is a value this is another value 123 4354353454

unpack() doesn't work for dictionaries (the type of table you're trying to print) if you did this it would print out nothing vvvvvvv

local date_table = os.date("!*t")
print(unpack(date_table))
Ad
Log in to vote
0
Answered by 4 years ago

There are multiple ways to sort through a table, and you seem to be taking the second approach. This is fine, however you are not properly indexing the table here s = s..tostring(t) but rather trying to concatenate the entire table you passed onto your string variable.

This can be fixed by changing:

s = s..tostring(t)

to:

s = s..tostring(t[i])

That said, I encourage you to learn about the for i, v in pairs(table) loop which is very handy for parsing through both tables and dictionaries.

Because os.date("!*t") returns a dictionary, your string can contain both the key AND the value of each index in the dictionary.

Example:

local function tableToString(t)
    local string = ""
    local index = 0
    for key, value in pairs(table) do
        index = index + 1
        string = string .. key ..": " .. tostring(value)
        if index == #table then
            string = string .. ", "
        end
    end
    return string
end

while true do
    local date_table = os.date("!*t")
    local converted = tableToString(date_table)
    print(converted)
    script.Parent.TextLabel.Text = converted
    wait (1)
end

There is of course an advantage to use the normal for loop because you don't need a seperate variable to keep track of your index but it's user preference I guess.

0
It returned this pack: function: the whole table probably antoniorigo4 117 — 4y
0
? FrogNinjaX 222 — 4y

Answer this question