You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gmod编写F4菜单时报错'end' expected near 'until'如何修复

错误原因与修复方案

你遇到的语法报错核心原因是将Gmod内置库util错误拼写为Lua保留关键字until,Lua解析器把until识别为循环语句的结束关键字,因此判断开头的if SERVER then没有对应的end闭合,抛出对应报错。

除此之外你的代码还有多处API拼写、大小写不规范问题,会导致后续功能无法正常运行,所有问题汇总如下:

  • 拼写错误:添加Garry's Mod钩子的函数是hook.Add,你误写为book.Add
  • 大小写问题:Gmod Lua API区分大小写,所有网络相关API都为首字母大写格式:util.AddNetworkString、net.Start、net.Send、net.Receive
  • 大小写问题:VGUI创建API为vgui.Create,基础窗口类名为DFrame而非Dframe
  • 逻辑遗漏:窗口关闭后没有清空全局frame变量,会导致下次按F4无法弹出窗口

修正后完整代码

if SERVER then 
    -- 修正util拼写、API大小写
    util.AddNetworkString("F4menu")
    
    function F4menufunc(ply)
        net.Start("F4menu")
        net.Send(ply)
    end
    -- 修正hook.Add拼写
    hook.Add("ShowSpare4", "F4menuHook", F4menufunc)
end 

if CLIENT then 
    -- 修正net.Receive大小写
    net.Receive("F4menu", function()
        if (!frame) then
            -- 修正vgui.Create和DFrame大小写
            local frame = vgui.Create("DFrame")
            frame:SetSize(1000,720)
            frame:Center()
            frame:SetVisible(true)
            frame:MakePopup()
            frame:SetDeleteOnClose(true)
            -- 补充关闭时清空变量逻辑,避免二次打开失败
            frame.OnClose = function()
                frame = nil
            end
        end
    end)
end

内容的提问来源于stack exchange,提问作者Senno

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 16:09:04