Spawn NPC from a button (clientside menu)

Local server but I’m told that still matters

My init file contains the following:

function GM:InitPostEntity()
hook.Add( "InitPostEntity", "some_unique_name", function()
	print( "Initialization hook called" )
end )

I have no idea if I’m using that correctly. My searches contained semi-contradictory information, ents.Create was removed in gmod 13 and so on. Pretty fragmented so I’m not sure if my syntax is even right.

This next one is part of a Doclick function that is working correctly for the other parts of it. (play a sound)

local spawnff = ents.Create('npc_citizen')
				if ( !IsValid( spawnff ) ) then return end // Check whether we successfully made an entity, if not - bail
				spawnff:SetModel( "models/npc/cdc_soldier_npc.mdl")
				spawnff:SetPos( Vector( 0, 0, 100 ) )

but I am getting a NIL field value error in console. I tried copying this exact block to the init but it never worked either. Some direction would be awesome.

You’ll need to look at the net library. You create a button, on the DoClick of it, send a message to the server telling it to create an entity. Hook to that message on the server and spawn it.

ents.Create is a function and was not removed, however it exists server side, while derma and buttons only exist on the client. Which is why things aren’t working out.

I tried using netlibrary for the sounds as well. I could never get it to work but I still have the code in there // .
in client.lua

--net.Start('send_playsound') net.WriteString("ENG101_Onscene.mp3")  

now in init.lua


local function PlaySound()
	local strSound = net.ReadString()
net.Receive("send_playsound", PlaySound)


Alright, so basically, in a server side file you’re going to want to pool the net message name, and set the receive hook then spawn it at the players eye trace hit pos, like so

net.Receive(“spawn_npc1”, function(l, ply)
local hit = ply:GetEyeTrace().HitPos

local ent = ents.Create("npc_citizen")


and in a client side file, you’re going to want to have your button(parented to your main panel!) which sends the net message upon click of it, like this
local btn = vgui.Create(“DButton”, MyParentPanel)
btn:SetSize(100, 50)
btn:SetText(“Spawn NPC”)
btn:SetPos(0, 0)
function btn:DoClick()

Awesome. It loaded without errors but when I tried to spawn I got this : Bad sequence (-1 out of 1 max) in GetSequenceLinearMotion() for model ‘error.mdl’!

EDIT. Reloaded and no errors but it doesn’t actually spawn it. Console doesn’t give me any clues to what’s going wrong?

change the model