How to spawn a prop in lua?

I am trying to make a vgui thng that spawns props, so far i have:
menu1 = vgui.Create(“DButton”) – Create the button
menu1:SetParent( Form ) – Set the parent to Form
menu1:SetVisible( true ) – Make the button true
menu1:SetSize( 100, 20 )
menu1:SetText( “Spawn an Exploding ball” )
menu1:SetPos( 5, 30 )
menu1:DoClick = function ( ball )
local prop = ents.Create( “prop_physics” )
prop:SetModel( “models/props_phx/cannonball_solid.mdl” )
prop:SetPos( ply:GetPos())
prop:Spawn()
` end

Also i think i might of messed up the doclick, is it ok?

You have missed the fact that you are attempting to spawn an entity clientside, which the client is not allowed to do. You must use the net library for this. Example:
[lua]
if SERVER then

util.AddNetworkString( "SpawnProp" )

net.Receive( "SpawnProp", function( length, ply ) // net.Receive() passes the length of the message and the player, so this is where you can get the player from

	local ent = ents.Create( "prop_physics" )
	ent:SetModel( net.ReadString() or "models/props_phx/cannonball_solid.mdl" ) // You can net.WriteString( model ) the model to the server
	ent:SetPos( ply:GetEyeTrace().HitPos ) // I'd also recommend spawning the prop where the player is looking at
	ent:Spawn()

end )

else

//	Derma stuff (clientside)
menu1.DoClick = function() 
	net.Start( "SpawnProp" )
	net.SendToServer()
end

end
[/lua]


 tags, use them.

Clientside:


menu1 = vgui.Create(“DButton”) – Create the button
menu1:SetParent( Form ) – Set the parent to Form
menu1:SetVisible( true ) – Make the button true
menu1:SetSize( 100, 20 )
menu1:SetText( “Spawn an Exploding ball” )
menu1:SetPos( 5, 30 )
function menu1:DoClick( ball )
net.Start( “doShit” )
net.SendToServer()
end




Serverside:



util.AddNetworkString(“doShit”)
net.Receive(“doShit”, function( len, ply )
local prop = ents.Create( “prop_physics” )
prop:SetModel( “models/props_phx/cannonball_solid.mdl” )
prop:SetPos( ply:GetEyeTrace().HitPos )
prop:Spawn()
end )

Im doing something wrong please could help me out? Im new to lua

Darn you, Ninja1001.

[editline]22nd February 2014[/editline]

Darn automerge.

:smiley:

[editline]22nd February 2014[/editline]

This is what i have now, stille errors in game:

menu1 = vgui.Create(“DButton”) – Create the button
menu1:SetParent( Form ) – Set the parent to Form
menu1:SetVisible( true ) – Make the button true
menu1:SetSize( 100, 20 )
menu1:SetText( “Spawn an Exploding ball” )
menu1:SetPos( 5, 30 )
function menu1:DoClick( ball )
net.Start( “doShit” )
net.SendToServer()

		util.AddNetworkString("doShit")

net.Receive(“doShit”, function( len, ply )
local prop = ents.Create( “prop_physics” )
prop:SetModel( “models/props_phx/cannonball_solid.mdl” )
prop:SetPos( ply:GetEyeTrace().HitPos )
prop:Spawn()
end )
end

You saw my post? You see that piece of code with a text above it says “serverside”? Clientside is where you create your panels and similar stuff. ( cl_init namely ). Serverside is where you can/must spawn your entities, etc, namely init.lua. Show us EXACTLY where do you put all this code into, what file, where is the file located.

it is in garrysmod/lua its called gui.lua and here is everything thats in it, its one file:

local Form = vgui.Create( “DFrame” )
Form:SetPos( 5, 5 ) – Position form on your monitor
Form:SetSize( 300, 150 ) – Size form
Form:SetTitle( “Scripts” ) – Form set name
Form:SetVisible( true ) – Form rendered ( true or false )
Form:SetDraggable( true ) – Form draggable
Form:ShowCloseButton( true ) – Show buttons panel
Form:MakePopup()

menu1 = vgui.Create(“DButton”) – Create the button
menu1:SetParent( Form ) – Set the parent to Form
menu1:SetVisible( true ) – Make the button true
menu1:SetSize( 100, 20 )
menu1:SetText( “Spawn an Exploding ball” )
menu1:SetPos( 5, 30 )
function menu1:DoClick( ball )
net.Start( “doShit” )
net.SendToServer()

		util.AddNetworkString("doShit")

net.Receive(“doShit”, function( len, ply )
local prop = ents.Create( “prop_physics” )
prop:SetModel( “models/props_phx/cannonball_solid.mdl” )
prop:SetPos( ply:GetEyeTrace().HitPos )
prop:Spawn()
end )
end

menu2 = vgui.Create(“DButton”) – Create the button
menu2:SetParent( Form ) – Set the parent to Form
menu2:SetVisible( true ) – Make the button true
menu2:SetSize( 100, 20 )
menu2:SetText( “Aimbot” )
menu2:SetPos( 5, 60 )
function menu2:DoCLick ( jump )
local bhop = { };
bhop.MetaPlayer = FindMetaTable( “Player”)
bhop.oldKeyDown = bhop.MetaPlayer[‘KeyDown’]
local bhopOn = true;
local bhopSOn = true;
local bhopHooks = { hook = { }, name = { } };
bhop.left = false
bhop.right = false
bhop.jump = false
bhop.what = true
function bhop.AddHook(hookname, name, func)
table.insert( bhopHooks.hook, hookname );
table.insert( bhopHooks.name, name );
hook.Add( hookname, name, func );
end
bhop.MetaPlayer[‘KeyDown’] = function( self, key )
if self != LocalPlayer() then return end
if (key == IN_MOVELEFT) and bhop.left then
return true
elseif (key == IN_MOVERIGHT) and bhop.right then
return true
elseif (key == IN_JUMP) and bhop.jump then
return true
else
return bhop.oldKeyDown( self, key )
end
end

                    function bhop.spamjump()
                            bhop.what = false
                            bhop.jump = true
                            timer.Simple(.05, function()
                                bhop.jump = false
                                timer.Simple( .25, function()
                                    bhop.what = true
                                end)
                            end)
                    end
 
     
        local oldEyePos = LocalPlayer():EyeAngles()--This is to see where player is looking
        function bhop.CreateMove( cmd )
                        local JumpReleased = false;
                if (cmd:KeyDown( IN_JUMP )) then
 
                        if (!JumpReleased) then
                                if (bhopOn && !LocalPlayer():OnGround()) then --Bhop here
                                        cmd:RemoveKey( IN_JUMP );
                                        if bhop.what then
                                            bhop.spamjump()
                                        end
                                end
                        else
                                JumpReleased = false
                        end
                                       
                                if(bhopSOn ) then--auto strafer
                                        local traceRes = LocalPlayer():EyeAngles()
                               
                                        if( traceRes.y > oldEyePos.y ) then --If you move your mouse left, walk left, if you're jumping
                                                oldEyePos = traceRes;
                                                cmd:SetSideMove( -1000000 )
                                                bhop.left = true
                                                timer.Simple(1, function() bhop.left = false end)
                                        end
         
                                        if( oldEyePos.y > traceRes.y )  then --If you move your mouse right, move right,  while jumping
                                                oldEyePos = traceRes;
                                                cmd:SetSideMove( 1000000 )
                                                bhop.right = true
                                                timer.Simple(1, function() bhop.right = false end)
                                        end
                                end
                elseif (!JumpReleased) then
                        JumpReleased = true;
                end
         
        end
           
        bhop.AddHook( "CreateMove", tostring(math.random(0, 133712837)), bhop.CreateMove )--add the hook
            concommand.Add( "bhop", function () --Toggler
                    if bhopOn then
                            print("Bhop off")
                            bhopOn = false;
                    else
                            print("Bhop on")
                            bhopOn = true;
                    end
            end )
            concommand.Add( "bhop_strafe",  function ()
                    if bhopSOn then
                            print("Strafe off")
                            bhopSOn = false;
                    else
                            print("Strafe on")
                            bhopSOn = true;
                    end
            end)
            concommand.Add("bhop_unload", function()
                for i = 1, #bhopHooks.hook do
                hook.Remove( bhopHooks.hook*, bhopHooks.name* );
                print( "Unhooked "..bhopHooks.hook*.." using name "..bhopHooks.name* );
            end
                    concommand.Remove("bhop_strafe")
                    concommand.Remove("bhop")
                    bhopOn = false;
                    bhopSOn = false;
                    print("Bhop unloaded")
                    table.Empty( bhopHooks )
                    concommand.Remove( "bhop_unload" )
            end)
  end

PANEL = {}

function PANEL:Init()
self:SetSize( 100, 100 )
self:Center()

end

function PANEL:Paint( w, h )
draw.RoundedBox( 0, 0, 0, w, h, Color( 0,0,0,255 ) )
end

It’s like you completely ignored what I said about


 tags. Create a file in lua/autorun/server/myfile.lua and put serverside stuff into it. Create a file in lua/autorun/client/myfile.lua and put clientside stuff into it.

Make a new file in autorun/server, this is where you can run server functions (like entity spawning). This is where you would put this:
[lua]util.AddNetworkString(“doShit”)
net.Receive(“doShit”, function( len, ply )
local prop = ents.Create( “prop_physics” )
prop:SetModel( “models/props_phx/cannonball_solid.mdl” )
prop:SetPos( ply:GetEyeTrace().HitPos )
prop:Spawn()
end )[/lua]

Now for a little info about ‘realms’.
If you place files in lua/autorun they will be run by both the client and the server. There is the autorun/server and autorun/client folders there to make sure only one runs it.
In a shared file, to prevent either realm from running functions that it can’t (which would cause errors) you would use a check like this:
[lua]
// This is shared code
if SERVER then
// This is server-side code
else
// This is clientside code
end
// This is also shared code
[/lua]
The global variable ‘SERVER’ is only defined server-side, meaning that the client would only run what is in the ‘else’ statement. The opposite applies to the ‘CLIENT’ variable.

You can check the realms of all functions in any wiki. If you’re using an old wiki, a yellow box would indicate client, and blue would indicate server. If there are both, then it is shared, and can be used in either. In the new wiki, there is a little bar near the bottom of the page with links referring to the sections the function belongs to. You can also see the realm it belongs to there:

I hope this was educational for you.

Oh thanks guys :smiley: