Hello! I am trying to get the ENT:Use hook to work on an NPC with no success. Here is the code:
function ENT:Use( activator, caller )
if( !activator:IsPlayer() ) then return end
print( "Player used NPC" )
umsg.Start( "NPC_Used", activator )
umsg.Short( self.Entity.shop )
umsg.End()
end
It doesn’t print that message or anything. I tried commenting out the if…then…end line, but still nothing.
Try this
Taken from Gmod Wiki!
[LUA]
function ENT:AcceptInput( Name, Activator, Caller )
if Name == “Use” and Caller:IsPlayer() then
umsg.Start(“ShopNPCUsed”, Caller) – Prepare the usermessage to that same player to open the menu on his side.
umsg.End() – We don’t need any content in the usermessage so we’re sending it empty now.
end
end
[/LUA]
Your code looks like it should work so the error is probably located somewhere else. When the game loads look up in the console to make sure there is no lua error tied to your files.
If you just want a working base there’s an example you can download included in this tutorial :
**[NPC Shop Tutorial
ENT.Use is not called for SNPCs by default.
[LUA]
function ENT:Use(activator, caller, type, value)
if ValidEntity(activator) && activator:IsPlayer() then
umsg.Start(“NPC_Used”, activator)
umsg.Short(self.shop)
umsg.End()
end
end
function ENT:AcceptInput(input, activator, caller, data)
if string.lower(input) == “use” then
self:Use(activator, caller, SIMPLE_USE)
end
end
[/LUA]