Entity spawns when i press a derma button?

Is there any way to have a entity spawn when I click a button on a derma menu besides ent_create? I read in a wiki that it doesn’t work in multiplayer.

It depends, if it’s to spawn stuff on servers others then your own then you will be limited to the console commands supplied by that server. What that means is that you can spawn any sandbox entity within the limits defined by the owner but if you go on some special gamemode that doesn’t allow entity spawning then you won’t be able to do anything.

The sandbox console commands you can use are gm_spawn, gmod_spawnnpc, gm_spawnsent, gm_giveswep, gm_spawnswep and gm_spawnvehicle.

They are detailed here : http://luabin.foszor.com/code/gamemodes/sandbox/gamemode/commands.lua

To call them use **[G.RunConsoleCommand

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=G.RunConsoleCommand)**.

[editline]1[/editline]
:eng101:

Ah, thanks for the help!

Oh I think I misread your post… If you are doing it for your own server then the best way to do it would be to create a console command on the server that you can use with previously mentionned RunConsoleCommand and in that console command you would use ents.Create to create your entity the way you want it to be.

**[Concommand.Add

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Concommand.Add)**
**[Ents.Create

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Ents.Create)** <— Freshly updated with examples if you have never spawned entities

Yeah, i am trying to do it for my server. And would i put Ents.Create in my gamemodes init.lua or my entitys init.lua?

On the server. Your entity is a description of it and the server is the one that creates it and spawns it into the world. Quick example time!

init.lua
[lua]local function SpawnMyEnt(ply,cmd,args)–These arguments are supplied to the function no matter what, but we’ll only use ply
if not ply:IsAdmin() then return end – This ends the function if the caller is not an admin, feel free to remove this line or to replace it with another check
local tr = ply:GetEyeTrace()
local ent = ents.Create(“myent”)–Class name of your entity here. This creates one instance of your entity
ent:SetPos(tr.HitPos)
ent:SetAndles(ply:GetAngles()) --This will make it face away from you.
ent:Spawn()–This makes it visible
end

concommand.Add(“SpawnMyEnt”,SpawnMyEnt)[/lua]
:science:

Ah, thanks for helping me!

I had forgotten the concommand.Add part!! I edited my post. Really important or it just won’t do anything. :smile:

It works thanks for the help!