打印 for 循环的值在 Lua 中输出 nil

Printing of for loop's value outputs nil in Lua

看看 for 循环word 其中的一部分。

local words = { 
    "One - Test", 
    "Two - Test", 
    "Three - Test"
}

local find = "Test"

local function getWord(partialName)
    partialName = partialName:lower()
    for _,word in ipairs(words) do

        if string.find(word:lower(),partialName) then
            print(word)
        end
    end
end

getWord(find)
Output:

One - Test
Two - Test
Three - Test

我正在尝试存储输出到其他变量的所有内容。 print(word) 输出你上面看到的,但是我怎么只能得到 一个 - 测试 结果并将其存储到另一个变量?我试过使用 print(word[1]) 来测试它,但它没有工作并输出 nil.

nil (x3)  -  Client - Local Script:14

现在我该如何解决?非常感谢!

无需打印结果,您只需将每个结果放入 table。

local words = { 
    "One - Test", 
    "Two - Test", 
    "Three - Test"
}
   
local find = "Test"

local function getWord(partialName)
    partialName = partialName:lower()
    local output = {}
    for _,word in ipairs(words) do 
        if string.find(word:lower(),partialName) then
            table.insert(output, word)
        end
    end
    return output
end

print(table.concat(getWord(find), "\n"))