Error on button push - What's wrong with this code?

Basically, I’m trying to make an entity that changes your playermodel without having to suicide. I have everything else working right, just not this part. When I select a model from the Derma panel, I get the error:

entities/gm_playermodel/cl_init.lua:41: attempt to index global 'self' (a nil value)


Here’s the cl_init.lua:
[lua] include(‘shared.lua’)

Name: Draw
Purpose: Draw the model in-game.
Remember, the things you render first will be underneath!

function ENT:Draw()
– self.BaseClass.Draw(self) – We want to override rendering, so don’t call baseclass.
– Use this when you need to add to the rendering.
self:DrawEntityOutline( 1.0 ) – Draw an outline of 1 world unit.
self.Entity:DrawModel() – Draw the model.


function openModelMenu()
local models = player_manager.AllValidModels( )

    local frame = vgui.Create("DFrame")
local IconList = vgui.Create( "DPanelList", frame ) 

frame:SetTitle("Select Your Model")

IconList:EnableVerticalScrollbar( true ) 
IconList:EnableHorizontal( true ) 
IconList:SetPadding( 4 ) 
IconList:SetSize(225, 460)

for k,v in pairs(models) do
local icon = vgui.Create( "SpawnIcon", IconList ) 
icon:SetModel( v )
IconList:AddItem( icon )
icon.DoClick = function( icon ) self.Entity:SetNetworkedString(1, v) end 


concommand.Add( “jm_modelmenu”, openModelMenu )[/lua]

Any idea? Sorry if it’s painfully obvious, I’m new to lua coding.
Oh, also, how exactly do you not make a console command/function run multiple times when you use an entity?

openModelMenu isn’t an entity hook; it has no arguments (it’s just a derma panel). self.Entity doesn’t exist in the function.

Any idea how I would go about getting the entity and using it in the function, then?

Uhh… perhaps you could try making a variable above the ENT:Draw() function (like just “local ent”), then say in ENT:Draw() “ent = self.Entity” and use ent in the menu function.