在lua(白名单)中搜索表格
问题描述:
我一直在尝试为我的Garry的Mod服务器编写白名单加载项。我对LUA相当陌生,因此非常感谢任何帮助。我有一个想法,但我不知道如何搜索它。说我有一张桌子在lua(白名单)中搜索表格
local Table = { Player1, Player2, Player3 }
hook.Add("PlayerConect", "Connect", function(ply)
if ply:Nick() != Table then
ply:Kick("Reason here")
end
end)
这是据我已经能够理解如何做。感谢您的时间。
答
我不熟悉盖瑞模组,但如果你只是需要检查,如果玩家的缺口是在表中,你可以这样做:如果你使用一个稍微不同的表来保存
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)
球员的缺口,那么检查将变得更简单(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)
+0
出于某种原因,我从控制台得到这个错误,说“字符串索引的错误键(期望的数字,得到的字符串)”我已经看了:Nick()命令给出了什么。它提供了一个字符串,并且表格被格式化为一个字符串表格,所以我不知道该从哪里去。我也修复了播放器连接没有PlayerConnect – Firefox52 2015-02-25 00:39:00
答
让列入白名单的SteamIDs的表(不要使用名字,他们不是唯一的!)
local WhitelistedIDs = {
["STEAM_0:0:52031589"] = true,
["STEAM_0:0:109379505"] = true,
["STEAM_0:0:115441745"] = true
}
然后编写代码应该是这样的,我没有使用PlayerConnect
钩
hook.Add("PlayerInitialSpawn", "MyAwesomeWhitelist", function(--[[ Player ]] player)
if (~WhitelistedIDs[player::SteamID()]) then
player:Kick("Sorry! You are not Whitelisted!")
end)
注意。我没有使用它,因为我们只有玩家的名字,但我们需要一个完整的玩家对象。
SOURE:我的经验和GMOD维基
注:例如,在使用SteamIDs都是我自己的有效账户 | 代码不testest,请注意,如果事情没有按预期工作
注:昵称可以改变。如果未经授权的用户发现服务器上允许的人的昵称,他们可以将其名称更改为该名称。你应该根据SteamIDs进行白名单。 – 2015-02-24 14:12:07