【问题标题】:Stop a table from inserting twice阻止表格插入两次
【发布时间】:2014-02-02 23:53:05
【问题描述】:

我有这个脚本

players={}
function eventNewGame()
        local playerList={}
    for name,tbl in pairs(players) do
        if tbl.wins>most.wins then
            most={name=name,wins=tbl.wins}
        end
    table.insert(log.cheese, "<j><b>"..most.name.."</b> <vp>won the round for gathering <ROSE><b>"..most.wins.." cheese!</b><N>")
end

但是,它打印/发送/插入的次数与房间里的人一样多。我怎样才能让它只插入一次?

【问题讨论】:

  • @Seagull 我恢复了您的编辑,因为它看起来像是故意将insert 行放入循环中。请不要进行有效改变问题含义的编辑。
  • @interjay 谢谢,我意识到错过了end,但为时已晚。

标签: lua lua-table


【解决方案1】:

您缺少for 循环的结束end,这导致insert 行位于循环内。你需要添加它:

for name,tbl in pairs(players) do
    if tbl.wins>most.wins then
        most={name=name,wins=tbl.wins}
    end
end      -- <---- add this
table.insert(log.cheese, "<j><b>"..most.name.."</b> <vp>won the round for gathering <ROSE><b>"..most.wins.." cheese!</b><N>")

最后你可能还有一个额外的end(否则你的代码将无法编译),你需要删除它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    相关资源
    最近更新 更多