简体   繁体   English

Garry's Mod-启动Lua脚本后道具不会产生

[英]Garry's Mod - Props won't spawn after launching Lua script

I have a test server for gmod. 我有一个用于gmod的测试服务器。 I've coded a script that works excellent when I launch it, but there is a lot of downsides with it. 我已经编写了一个脚本,该脚本在启动时效果很好,但是它有很多缺点。

I've tried to code a script that will simply change the users speed if they type a command like "!speed fast", or "!speed normal". 我试图编写一个脚本,如果用户键入“!speed fast”或“!speed normal”之类的命令,它们只会改变用户的速度。 It looks like this: 看起来像这样:

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)

As you can see the script change the users speed if they either type "!speed normal", "!speed fast" or "!speed sanic" in chat. 如您所见,如果用户在聊天中键入“!speed normal”,“!speed fast”或“!speed sanic”,则脚本会更改用户的速度。 The script also contains a table of every command, and it will be shown if the user type "!help" in chat. 该脚本还包含每个命令的表,如果用户在聊天中键入“!help”,则会显示该表。

When I launch the script it works excellent, but if I try to spawn a prop after I've launched it, the prop won't spawn. 当我启动脚本时,它的工作效果非常好,但是如果我在启动脚本后尝试生成道具,则该道具将不会生成。 Even when I spawn a prop first, then launch the script and try to "undo" the prop, the "undo" function won't work! 即使当我先生成一个道具,然后启动脚本并尝试“撤消”道具时,“撤消”功能也将不起作用! The script makes Sandbox gamemode completely useless, because you can't even spawn props! 该脚本使Sandbox游戏模式完全无用,因为您甚至无法生成道具!

I've tried to search a little bit around on the internet first, but I haven't stumbled across something like this yet, so I hope someone got the solution! 我尝试先在互联网上进行一些搜索,但是还没有偶然发现这样的东西,所以我希望有人能找到解决方案! Please help 请帮忙

My guess is that this is happening because you are overwriting the global table . 我的猜测是这种情况正在发生,因为您正在覆盖全局table The table library contains helper functions for tables . 表库包含表的辅助函数 Try renaming your table table to something else, like commands . 尝试重新命名表table到别的东西,像commands I would also suggest you declare it as local commands so it does not replace any other global, so it does not interfere with anything else, like other scripts or library. 我还建议您将其声明为local commands以便它不会替代任何其他全局变量,因此它不会干扰其他任何内容,例如其他脚本或库。

Also, as extra tips, lua tables are indexed with 1, So you could declare your renamed table as: 另外,作为额外提示,lua表的索引为1,因此您可以将重命名的表声明为:

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

You could then iterate over it with a normal for : 然后,您可以使用正常的for迭代:

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

I think this makes it a bit cleaner, in my opinion. 我认为,这会使它变得更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM