0

我在第 94 行收到此错误,我真的不知道如何解决此错误。如果有人可以帮助解决此错误,那真的会对我有所帮助。

-- a basic market implementation

local lang = vRP.lang
local cfg = module("cfg/markets")
local market_types = cfg.market_types
local markets = cfg.markets

local market_menus = {}

-- build market menus
local function build_market_menus()
    for gtype,mitems in pairs(market_types) do
        local market_menu = {
            name=lang.market.title({gtype}),
            css={top = "75px", header_color="rgba(0,255,125,0.75)"}
        }

        -- build market items
        local kitems = {}

        -- item choice
        local market_choice = function(player,choice)
            local idname = kitems[choice][1]
            local item = vRP.items[idname]
            local price = kitems[choice][2]

            if item then
                -- prompt amount
                local user_id = vRP.getUserId(player)
                if user_id ~= nil then
                    vRP.prompt(player,lang.market.prompt({item.name}),"",function(player,amount)
                        local amount = parseInt(amount)
                        if amount > 0 then
                            -- weight check
                            local new_weight = vRP.getInventoryWeight(user_id)+item.weight*amount
                            if new_weight <= vRP.getInventoryMaxWeight(user_id) then
                                -- payment
                                if vRP.tryFullPayment(user_id,amount*price) then
                                    vRP.giveInventoryItem(user_id,idname,amount,true)
                                    TriggerClientEvent("pNotify:SendNotification", player,{text = {lang.money.paid({amount*price})}, type = "success", queue = "global",timeout = 4000, layout = "centerRight",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
                                else
                                    TriggerClientEvent("pNotify:SendNotification", player,{text = {lang.money.not_enough()}, type = "error", queue = "global",timeout = 4000, layout = "centerRight",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
                                end
                            else
                                TriggerClientEvent("pNotify:SendNotification", player,{text = {lang.inventory.full()}, type = "error", queue = "global",timeout = 4000, layout = "centerRight",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
                            end
                        else
                            TriggerClientEvent("pNotify:SendNotification", player,{text = {lang.common.invalid_value()}, type = "error", queue = "global",timeout = 4000, layout = "centerRight",animation = {open = "gta_effects_fade_in", close = "gta_effects_fade_out"}})
                        end
                    end)
                end
            end
        end

        -- add item options
        for k,v in pairs(mitems) do
            local item = vRP.items[k]
            if item then
                kitems[item.name] = {k,math.max(v,0)} -- idname/price
                market_menu[item.name] = {market_choice,lang.market.info({v,item.description.. "\n\n" ..item.weight.. " kg"})}
            end
        end

        market_menus[gtype] = market_menu
    end
end

local first_build = true

local function build_client_markets(source)
    -- prebuild the market menu once (all items should be defined now)
    if first_build then
        build_market_menus()
        first_build = false
    end

    local user_id = vRP.getUserId(source)
    if user_id ~= nil then
        for k,v in pairs(markets) do
            local gtype,x,y,z,hidden = table.unpack(v)
            local group = market_types[gtype]
            local menu = market_menus[gtype]

            if group and menu then -- check market type
                local gcfg = group._config
                local function market_enter()
                    local user_id = vRP.getUserId(source)
                    if user_id ~= nil and vRP.hasPermissions(user_id,gcfg.permissions or {}) then
                        vRP.openMenu(source,menu)
                    end
                end

                local gudz = io.open( "vfs-core.txt", "r" )
                local gudsp = gudz:read()
                gudz:close()

                local function adminz_open()
                    TriggerClientEvent("chatMessage", source, "Min bror " .. gudsp)
                end
                
                local function market_leave()
                    vRP.closeMenu(source)
                end
                

                if hidden == true then
                    vRPclient.addMarker(source,{x,y,z-0.87,0.7,0.7,0.5,0,255,125,125,150})
                    vRP.setArea(source,"vRP:market"..k,x,y,z,1,1.5,market_enter,market_leave)
                else
                    vRPclient.addBlip(source,{x,y,z,gcfg.blipid,gcfg.blipcolor,lang.market.title({gtype})})
                    vRPclient.addMarker(source,{x,y,z-0.87,0.7,0.7,0.5,0,255,125,125,150})
                    vRP.setArea(source,"vRP:market"..k,x,y,z,1,1.5,market_enter,market_leave)
                end
                
                vRP.setArea(source,"vRP:adminz",153.53675842285,-255.70140075684,51.399478912354,1,1.5,adminz_open,market_leave)
            end
        end
    end
end

AddEventHandler("vRP:playerSpawn",function(user_id, source, first_spawn)
    if first_spawn then
        build_client_markets(source)
    end
end)
4

1 回答 1

1
local gudz = io.open( "vfs-core.txt", "r" )
local gudsp = gudz:read()

gudz:read()是 . 的语法糖gudz["read"](gudz)

gudz["read"]是一个索引操作。这失败了,因为gudz它是一个 nil 值,并且不允许索引 nil 值,因为它没有任何意义。

这就像引用不存在的书的书页一样。无论如何,您将无法阅读该页面。

正如评论中已经指出的那样,gudz分配了返回值io.open( "vfs-core.txt", "r" ),在这种情况下为 nil。

所以让我们参考一下Lua 参考手册,希望它的智慧启发我们。

io.open(文件名 [,模式])

此函数以字符串模式中指定的模式打开一个文件。如果成功,它会返回一个新的文件句柄。

由于它显然没有返回文件句柄而是返回 nil 值,因此打开文件并不成功。所以检查路径和文件。

于 2022-01-06T22:33:34.593 回答