A useable NPC

Hello.

What I need is a simple npc_citizen entity with just a normal idle animation and when you press E on it, it sends this:


caller:OpenWholesaleMenu();

I’ve done it by setting a model and then setting the animation, but your only able to press E on it at it’s feet, and it doesn’t stand correctly when un-frozen.

First, spawn the npc, on the server. InitPostEntity would be the right place.

[lua]
local NPC = ents.Create(“npc_citizen”)
NPC:SetPos(Location)
NPC:SetAngles(Angles)
NPC:Spawn()
NPC.LolName = true
NPC:SetModel(Model)
NPC:SetCollisionGroup( COLLISION_GROUP_WORLD )
[/lua]

Fill in the blanks.

Then:

[lua]
function GM:KeyPress ( Player, Key )
if Key == IN_USE then
local tr = {}
tr.start = Player:GetShootPos()
tr.endpos = tr.start + Player:GetAimVector()*50
tr.filter = Player
tr = util.TraceLine(tr)
if tr.MatType == MAT_GLASS then
local start = tr.HitPos
tr = {}
tr.start = start + Player:GetAimVector()*20
tr.endpos = tr.start + Player:GetAimVector()*50
tr = util.TraceLine(tr)
end

	if tr.Entity and tr.Entity:IsValid() and tr.Entity:GetPos():Distance(Player:GetPos()) < 100 then
		if tr.Entity:IsNPC() and tr.Entity.LolName then
			umsg.Start('talktonpc', Player)
			umsg.End()
		end
	end
end

end
[/lua]

And on the client:

[lua]
local function LetsOpenAMenuForTheNPC( msg )
//OpenWholesaleMenu()
//just stick your code in here…
end
usermessage.Hook( “talktonpc”, LetsOpenAMenuForTheNPC )
[/lua]

Should work, the code was mashed together from places unknown however.

Yes! Thankyou. Your a fucking genius.