Garry's Mod - 启动 Lua 脚本后无法生成道具

我有一个用于 gmod 的测试服务器。我编写了一个非常优秀的脚本,当我启动它时它可以很好地运行,但是它也有很多缺点。

我尝试编写一个脚本,如果用户在聊天中输入像“!speed fast”或“!speed normal”等命令,它将简单地更改用户的速度。脚本看起来像这样:

table = {}
table[0]="!help"
table[1]="!speed normal"
table[2]="!speed fast"
table[3]="!speed sanic"
hook.Add("PlayerSay", "Chat", function(ply, message, teamchat)
    if message == "!speed normal" then
        GAMEMODE:SetPlayerSpeed(ply, 250, 500 )
    elseif message == "!speed fast" then
        GAMEMODE:SetPlayerSpeed(ply, 1000, 2000 )
    elseif message == "!speed sanic" then
        GAMEMODE:SetPlayerSpeed(ply, 10000, 20000)
    elseif message == "!help" then
        for key, value in pairs(table) do
            PrintMessage( HUD_PRINTTALK, value)
        end
    end
end)

正如你所看到的,如果用户在聊天中输入“!speed normal”、“!speed fast”或“!speed sanic”,脚本将更改用户的速度。脚本还包含每个命令的表格,如果用户在聊天中输入“!help”,则会显示表格。

当我启动脚本时,它的表现很好,但是如果我尝试生成道具,则道具不会出现。即使我先生成一个道具,然后启动脚本并尝试“撤消”道具,也无法执行“撤消”功能!该脚本使沙盒游戏模式完全无用,因为你甚至无法生成道具!

我尝试在互联网上搜索了一些信息,但我还没有遇到这样的情况,因此我希望有人可以提供解决方案!请帮帮我。

点赞
用户1381216
用户1381216

我的猜测是这种情况发生的原因是您正在覆盖全局 table。[table 库包含表的帮助函数](https://www.lua.org/pil/19.html)。尝试将您的表 table 重命名为其他名字,例如 commands。我还建议您将其声明为 local commands,这样它就不会替换任何其他全局环境,以免干扰其他脚本或库。

另外,作为额外的提示,lua 表以1为索引。因此,您可以将重命名后的表声明为:

local commands = {
    "!help",
    "!speed normal",
    "!speed fast",
    "!speed sanic",
}

然后,您可以使用常规的 for 进行迭代:

for index = 1, #commands do
    PrintMessage(HUD_PRINTTALK, commands[index])
end

我认为这会使代码看起来更加清晰。

2016-10-05 21:54:54