Hello, I would like to know how to make it that if I type: !spawncitizen ingame or something, it will spawn a citizen right near me. Thank you in advance :)
Something like the following should get you started ;)
[lua]
function ChatCommandNPC(ply, text)
if ( string.sub(text, 1, 12) == "!somecommand" ) -- Check first 12 characters of text to check for !somecommand.
ply:ConCommand("spawnnpc") -- If it's found, force the player to run the spawnnpc command.
return "" -- Return "" so the command doesn't print to chat.
end
end
hook.Add("PlayerSay", "ChatCommandNPC", ChatCommandNPC)
function SpawnSomeNPC (ply, command, args)
if ply:IsAdmin() then -- Simple admin check, so other players can't spawn npcs.
local tr = ply:GetEyeTrace() -- Basically, get where the player is aiming right now.
local somenpc = ents.Create("npc_citizen") -- Create the npc_citizen entity and call it "somenpc".
somenpc:SetPos(tr.HitPos) -- set somenpc's position to where the player's aim hits the world.
ent:Spawn() -- finally spawn the npc.
else
ply:ChatPrint("You are not admin!")
end
end
concommand.Add("spawnnpc", SpawnSomeNPC) -- add a console command for the function created above.[/lua]
Untested, but that should simply spawn a citizen where your crosshair hits the world. If you want to make it actually spawn near the player, you could get the position of player, then +Vector(50,0,0) or something, but that could cause issues when near walls, etc, etc.
Sorry, you need to Log In to post a reply to this thread.