attempt to index global 'INV' (a nil value) (line:23)

374 Views Asked by At

print("cl_inv wurde geladen!")

concommand.Add("inv_model", function(ply)

    local wep = ply:GetActiveWeapon()
    if not IsValid(wep) then return end

    print(wep:GetWeaponWorldModel())

end)

net.Receive("inv_init", function()
    LocalPlayer().inv = {}
end)

net.Receive("inv_give", function()
    local classname = net.ReadString()
    table.insert(LocalPlayer().inv, classname)

end)

function INV.Open()
    local ply = LocalPlayer()
    local plyinv = ply.inv
    if not ply.inv then return end

    local scrw, scrh = ScrW(),ScrH()

    INV.Menu = vgui.Create("DFrame")
    local inv = INV.Menu
    inv:SetSize(scrw * .4, scrh * .6)
    inv:Center()
    inv:SetTitle("")
    inv:MakePopup()

    inv.Paint = function(me,w,h)
        surface.SetDrawColor(0,0,0,200)
        surface.DrawRect(0,0,w,h)
    end

    local scroll = inv:Add("DScrollPanel")
    scroll:Dock(FILL)
    scroll.panels = {}
    for k,v in pairs(plyinv) do
        local itemPanel = scroll:Add("DPanel")
        itemPanel:Dock(TOP)
        scroll.panels[itemPanel] = true
    end


end

hook.Add( "OnPlayerChat", "InvOpen", function( ply, strText, bTeam, bDead )
    if ( ply != LocalPlayer() ) then return end

    if string.lower(strText) != "!inv" then return end

    INV.Open()
end )


I already searched the whole internet for a solution, i did found something but it didnt really helped me so what im expecting is that someone may be so nice and can help me solve my problem. In line 23 is the error :attempt to index global 'INV' (a nil value) (line:23)

1

There are 1 best solutions below

0
On

As I can see, you're using the Garry's Mod Lua API. Although this API adds some features to the basic Lua language (e.g. ! operator for negation, C-style comments //), you still need to use the Lua language properly.

What the error says, is that you're trying to access to a table that isn't defined in the current scope of your program.

I can bet your addon defined some global table to interoperate between files and other addons installed on your machine.

As I don't have that much information on the way you're loading your file, here are multiple guesses on possible solutions:

For a file located inside an autorun folder

If the snippet you gave is under an autorun folder, the issue may be that the INV table does not yet exist when you load your file. To correct that, you can use the GLua Timer's Library.

Here is a simple snippet:

Timer.Simple(0.2, function()
 -- put your code here
end

Timer.Simple takes two parameters: first one is a delay, in seconds and the other one is a callback function, executed once the delay has ended !

If you're doing something that requires the server to have loaded some other addons before actually running your script, this might be helpfull.

Accessing Global variables of your environment

As you did not gave that many informations about your problem I have to continue to guess what you're trying to do. An other source of problem is maybe that the INV table simply doesn't exist.

How to get all defined global tables in Lua ?

In Lua, there is the _G table that contains all the data of your current environnement. For more in-depth information, please see the official Lua documentation.

To know if your table does in fact exist in your environment, you can run this snippet on your Lua server:

local function doesINVExist()
    for name in pairs(_G) do
        if (name == "INV") then return true end
    end

    return false
end

And then you can simply run it using:

print(doesINVExist())

What to do if the table doesn't exist

If INV isn't present in your environment, then it may be because your Garry's Mod server did not loaded any file that defines such a table. Then what you can do is checking the addon that should dclare that table to see if there are any errors that might make it a nil value.


I hope this can help. Of course my answer could've been much more detailed if I had more information. But as I can't write comments (my rating isn't high enough), here is all I can do for you !

Have a great day, Pedro.