Lua 显示表格

我创建了一个有多个选项的测验。在开始游戏之前,用户必须使用一个标识符,然后用户将被添加到表中并准备好玩:

function addUser(msg)
  local id = msg.from.username
  if (userScore == nil) then
     userScore = {}
  end
  if (userScore[id]) then
     return "用户已经在游戏中"
  else
       userScore[id] = 100
     return id
  end

这将在表中添加分数:

function addScore(msg)
  local id = msg.from.username

 if (userScore[id] == nil) then
     return "未知用户。开始游戏吧!"
  end
 if (game == "on") then
     if (userScore[id]) then
        userScore[id] = userScore[id] - 1
         return id .. ",扣除一分!"
     else
       return id .. "尚未成为用户!从哪里开始?"
     end
  else
    return "游戏模式关闭"
  end
end

然后使用 !score 用户可以看到得分:

elseif (matches[1] == "!score") then
  for k, v in pairs(userScore) do
   return k .. " : " ..  v
  end

我遇到的问题是我只看到表格中的一行,尽管其他用户已添加到表格中。我做错了什么?

点赞
用户3586583
用户3586583

从评论中看来,似乎您想要返回一个包含 userScore 表中键-值对的单个字符串,并且每行显示一组。

您可以通过构造一个具有这些行的字符串来实现此目的。例如:

local res = {}
for k, v in pairs(userScore) do
    table.insert(res, k .. " : " ..  v)
end
return table.concat(res, "\n")
2015-12-12 20:44:09