Error with an entity

Trying to make a money printer. Having trouble understanding some things…

I’m trying to make it so that when you press and hold E, a timer starts. When that timer is over, it executes a function which levels up the printer. If you release E, the timer is destroyed.

Full init.lua: http://pastebin.com/BZsvERF4
Part of my init.lua:
[LUA]
function ENT:upgrade()
print( “-----
Old Level: " … self:GetLevel() … "
-----” )
self:SetLevel( 2 )
print( " New Level: " … self:GetLevel() )
if timer.Exists( “upgrade” ) then timer.Destroy( “upgrade” ) end
end

function beginUpgrade( ply, key )
if ( key == IN_USE ) then
print( “Holding…
-----” )
timer.Create( “upgrade”, 3, 1, function() upgrade() end )
end
end
hook.Add( “KeyPress”, “beginUpgrade”, beginUpgrade )

function stopUpgrade( ply, key )
if ( key == IN_USE ) then
if timer.Exists( “upgrade” ) then timer.Destroy( “upgrade” ) end
print( “Not upgraded!
-----” )
end
end
hook.Add( “KeyRelease”, “stopUpgrade”, stopUpgrade )
[/LUA]

shared.lua:
[LUA]
ENT.Type = “anim”
ENT.Base = “base_gmodentity”
ENT.PrintName = “Scrub Printer”
ENT.Spawnable = false
ENT.AdminSpawnable = false

function ENT:SetupDataTables()
self:NetworkVar(“Int”, 0, “price”)
self:NetworkVar(“Entity”, 0, “owning_ent”)
self:NetworkVar(“Int”, 0, “Level”)
end
[/LUA]

The error I’m getting is "attempt to call global ‘upgrade’ (a nil value)"

I’ve tried other things but nothing seems to work. Tried using self but it was never able to call that either… Very confused… Thanks in advance for any insight.

You’re creating upgrade as a metamethod to your entity. You’ll need to call it on your entity, not as a normal function like you do in your upgrade timer.
You’ll either need to get the trace entity in order to locate the entity the player is pressing E on or use this (recommended): http://wiki.garrysmod.com/page/ENTITY/Use
In this function, you can (after your checks) create the timer like this:
[lua]
timer.Create(“upgrade_”…self:EntIndex(), 3, 1, function() self:upgrade() end)
[/lua]
I’ve made the timer identifier unique to the entity, since you’d get complications when other people try to upgrade their entity at the same time if you don’t.

Awesome! Thank you!