ENT:Use

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 replacing if( !activator:IsPlayer() ) then return end with if activator:IsValid() and activator:IsPlayer() then
<code here>
end

have you checked the console for errors?

Yes I have checked, and no, the if statement isn’t the problem. I commented it out and it still didn’t work.

Also, no errors.

Is it a SNPC you’re scripting or are you trying to add to an existing NPC? Because you can’t do that if that’s the case.

Its an SNPC.

Make sure you have a variable in the same or shared script as what you posted that is named ENT.Entity.shop

That isn’t the problem. It won’t even run the print() function.

No one has any idea what might be wrong?

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]

With Lua Tags ^^

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

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=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]

I just saw that a little while ago :stuck_out_tongue:

Thanks anyway everyone!

Does it work now?