• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Can you execute from table?

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
74
I want to print ABCDE if I choose key [1]

LUA:
    local testB = {
    [1] = {
    {print("A")},
    {print("B")},
    {print("C")},
    {print("D")},
    {print("E")}
},
    [2] = {
}
}
   testB[1]
 
Solution
LUA:
local list =
{
    [1] = function ()
        print("A")
        print("B")
        print("C")
        print("D")
    end,
    [2] = function ()
    end
}

if type(list[1]) == "function" then
    list[1]()
end
I want to print ABCDE if I choose key [1]

LUA:
    local testB = {
    [1] = {
    {print("A")},
    {print("B")},
    {print("C")},
    {print("D")},
    {print("E")}
},
    [2] = {
}
}
   testB[1]

Not sure if you're going for a function or just calling the table but here's a few examples.

LUA:
testTable = {
    [1] = "ABCDE"
}

function testFunc(value)
    if testTable[value] then
        print(testTable[value])
    end
end

print(testTable[1])
testFunc(1)
 
LUA:
local testTable = {
    [1] = {function() print("A") end, function() print("B") end}
}

testTable[1][1]() -- prints A

-- to print all:
for i = 1, #testTable do
    for j = 1, #testTable[i] do
        testTable[i][j]()
    end
end
 
LUA:
local list =
{
    [1] = function ()
        print("A")
        print("B")
        print("C")
        print("D")
    end,
    [2] = function ()
    end
}

if type(list[1]) == "function" then
    list[1]()
end
 
Last edited:
Solution
OMG Thx Guys

Something was missing as it was executing print statement even by sitting in the table

but we need function to prevent them from doing so

now I get it

!
thx

just needed that as I have multiple strings to execute at different times
 
it got executed because you're calling print directly, not placing a value into the table. print returns nil so your table was filled with 5 nil values
if one of us solved your problem you can click the "best answer" button on the post next to the buttons on the bottom right
 
it got executed because you're calling print directly, not placing a value into the table. print returns nil so your table was filled with 5 nil values
if one of us solved your problem you can click the "best answer" button on the post next to the buttons on the bottom right

but print["A"] was in a table
 
For example:
LUA:
local table = {
"A", "B", "C", "D" }

for s = 0, #table do
print(table[s])
end

@Sarah Wesker Just want to help. You should start "s" from 1. On Lua, the first array's index is 1, not 0.
There are a few ways to do this loop.

local table = { "A", "B", "C", "D" } for k = 1, #table do print(table[k]) end -- Loop through known numeric indexes
local table = { "A", "B", "C", "D" } for key, value in ipairs(table) do print(value) end -- Loop from index 1 to 'n' (n is the last index of table). Only when keys are numeric and start from 1 to 'n' as your table
local table = { "A", "B", "C", "D" } for key, value in pairs(table) do print(value) end -- Randomly loop, when the order doesn't matters. It will includes the ones with numeric keys and string keys, no matter if it has jumps on the numeric keys, no matter if the numeric keys is negative or has the key 0, or string as the keys.
 
but print["A"] was in a table
when you call a function, you get a value returned back after it's executed
in this case, you're calling print("A"), and print returns nil no matter what
{print("A")} is the same as {nil}, not {"A"} or {function print("A") end}
here's an example to show you what i mean
LUA:
function myFunc()
    return 5
end

local sometable = {
    myFunc(),
    myFunc(),
    myFunc()
} -- {5, 5, 5}

local othertable = {
    function() return myFunc() end,
    function() return myFunc(), end,
    function() return myFunc() end
} -- {function, function, function}

print(sometable[1]) -- prints 5
print(othertable[1]) -- prints function: (memory address the function is stored at)
print(othertable[1]()) -- prints 5, since othertable[1] is a function waiting for execution
 
Back
Top