阻止表格插入两次。

我有这个脚本

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

但是,它会打印/发送/插入像房间中的人数一样多次。我该如何使它仅插入一次?

![Enter image description here] (https://i.stack.imgur.com/jI6Z6.png)

点赞
用户189205
用户189205

你缺少了 for 循环的闭合语句 end,导致 insert 行在循环内。你需要加上它:

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

你可能还多了一个额外的 end,否则你的代码将无法编译,你需要将其删除。

2014-02-02 15:47:30