Lua:表预计,没有

所以,我在尝试将字符串分成表格(玩家加入团队)时遇到了问题.当只有两个玩家时,它就像一个魅力,但当有3个玩家时,会弹出:“Init Error:transformice.lua:7:bad argument:table expected,no nil”.一切似乎都没问题,我真的不知道出了什么问题.你能帮我吗?谢谢!这是我的代码
ps = {"Player1","Player2","Player3","Player4"}
local teams={{},{},{}}

--[[for name,player in pairs(tfm.get.room.playerList) do
 table.insert(ps,name)
 end]]

table.sort(ps,function() return math.random()>0.5 end)
for i,player in ipairs(ps) do
  table.insert(teams[i%#teams],player)
  end

解决方法

Lua数组从索引1开始,而不是0.如果你有3个玩家,那么这一行:
table.insert(teams[i%#teams],player)

评估为:

table.insert(teams[3%3],player)

那将最终成为:

table.insert(teams[0],player)

而团队[0]将是零.你应该能够把它写成:

table.insert(teams[i%#teams+1],player)

代替.

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...