在lua(白名单)中搜索表

时间:2015-02-24 06:20:30

标签: lua lua-table garrys-mod

我一直在尝试为Garry的Mod服务器编写白名单加载项。我是LUA的新手所以非常感谢任何帮助。我有一个想法,但我不知道如何搜索它。说我有一张桌子

local Table = { Player1, Player2, Player3 }
hook.Add( "PlayerConect", "Connect", function(ply)
       if ply:Nick() != Table then
       ply:Kick( "Reason here" )
   end
end)

据我所知,这是怎么做的。 谢谢你的时间。

2 个答案:

答案 0 :(得分:1)

我不熟悉Garry的Mod,但如果您只是需要检查玩家的昵称是否在表中,您可以这样做:

local Table = { "Player1", "Player2", "Player3" }
hook.Add( "PlayerConect", "Connect", function(ply)
     local notfound = true
     -- iterate through all elements in the table
     for index, nick in ipairs(Table) do
       if ply:Nick() == nick then
         notfound = false
         break
       end
     end
     if notfound then ply:Kick( "Reason here" ) end
end)

如果您使用稍微不同的牌桌来容纳玩家' nicks,然后检查会变得更简单(Table现在用作hash table):

local Table = { Player1 = true, Player2 = true, Player3 = true }
hook.Add( "PlayerConect", "Connect", function(ply)
     -- check if the nick is present in the table
     if not Table[ply:Nick()] then ply:Kick( "Reason here" ) end
end)

答案 1 :(得分:0)

制作列入白名单的SteamID表(不要使用名称!它们不是唯一的)

local WhitelistedIDs = {
  ["STEAM_0:0:52031589"] = true,
  ["STEAM_0:0:109379505"] = true,
  ["STEAM_0:0:115441745"] = true
}

然后编写你的代码应该是那样的

hook.Add( "PlayerInitialSpawn", "MyAwesomeWhitelist", function( --[[ Player ]] player)
if (~WhitelistedIDs[player::SteamID()]) then
   player:Kick( "Sorry! You are not Whitelisted!" )
end)

请注意,我没有使用PlayerConnect Hook。我没有使用它,因为我们只有玩家的名字,但我们需要一个完整的玩家对象。

Soure:我的经历和GMod Wiki

注意:示例中使用的SteamID都是我自己的有效帐户 | 代码不是testest,如果某些内容无法正常工作,请发表评论