Problem using gm_spawn (please help me!)

Hi, I am very new to lua for gmod and need some help to use, or fix this
code:

What it ahould do: make a spawn menu where you can spawn some different props.
I found the code at gmod wiki.

----LUA----
function fridge()
local food = {}

    food[1] = "models/props_junk/garbage_milkcarton002a.mdl"
food[2] = "models/props_junk/PopCan01a.mdl"
food[3] = "models/props_junk/garbage_takeoutcarton001a.mdl"
food[4] = "models/props_junk/watermelon01.mdl"
food[5] = "models/props_junk/garbage_metalcan001a.mdl"
food[6] = "models/props_lab/box01a.mdl"
food[7] = "models/props_lab/box01b.mdl"

    local frame = vgui.Create("DFrame")
local IconList = vgui.Create( "DPanelList", frame ) 

frame:Center()
frame:SetSize(220,200)
frame:SetTitle("Fridge")
frame:MakePopup()

IconList:EnableVerticalScrollbar( true ) 
IconList:EnableHorizontal( true ) 
IconList:SetPadding( 4 ) 
IconList:SetPos(10,30)
IconList:SetSize(200, 160)

for k,v in pairs(food) do
local icon = vgui.Create( "SpawnIcon", IconList ) 
icon:SetModel( v )
IconList:AddItem( icon )
icon.DoClick = function( icon ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gm_spawn", v) end 
end 	

end
fridge()
------LUA----


(User was banned for this post ("Wrong section" - mahalis))

What’s wrong with it?

Also the correct way to post lua code is:

[noparse][lua]
CODEHERE
[/lua][/noparse]

And another thing, this is the wrong section, should go in Questions.

Hold on, ill copy the error:…

[editline]09:18PM[/editline]

Unknown command: gm_spawn

I presume you’re using this in a gamemode of your own.

To fix this simply put:

[lua]DeriveGamemode(“sandbox”)[/lua]

At the top of your shared.lua file.

And,when I run it from the file in the lua folder.

food.lua:12: attempt to index global ‘vgui’ (a nil value)

[editline]09:21PM[/editline]

I`ll test!

[lua]RunConsoleCommand("gm_spawn ", v) [/lua]

Cubar that dosn’t help at all, just leave.

And that vgui error you had is because you were running it from the server, run it from the client and it’ll work fine.

Ok, but now the gamemod doesent work. -.-*

[editline]09:30PM[/editline]

cl_init
[lua]
include( “shared.lua” )

function set_team()

frame = vgui.Create( “DFrame” )
frame:SetPos( 100, ScrH() / 2 ) //Set the window in the middle of the players screen/game window
frame:SetSize( 200, 210 ) //Set the size
frame:SetTitle( “Choose Team” ) //Set title
frame:SetVisible( true )
frame:SetDraggable( true )
frame:ShowCloseButton( true )
frame:MakePopup()

team_1 = vgui.Create( “DButton”, frame )
team_1:SetPos( 30, 30 )
team_1:SetSize( 100, 50 )
team_1:SetText( “Engineer” )
team_1.DoClick = function() //Make the player join team 1

 RunConsoleCommand( "team_1" ) 

end

team_2 = vgui.Create( “DButton”, frame )
team_2:SetPos( 30, 85 ) //Place it next to our previous one
team_2:SetSize( 100, 50 )
team_2:SetText( “Powerful dude” )
team_2.DoClick = function() //Make the player join team 2

 RunConsoleCommand( "team_2" ) 

end

team_3 = vgui.Create( “DButton”, frame )
team_3:SetPos( 30, 140 ) //Place it next to our previous one
team_3:SetSize( 100, 50 )
team_3:SetText( “Solider” )
team_3.DoClick = function() //Make the player join team 2

 RunConsoleCommand( "team_3" ) 

end
end
concommand.Add( “team_menu”, set_team )

function fridge()
local food = {}

    food[1] = "models/props_junk/garbage_milkcarton002a.mdl"
food[2] = "models/props_junk/PopCan01a.mdl"
food[3] = "models/props_junk/garbage_takeoutcarton001a.mdl"
food[4] = "models/props_junk/watermelon01.mdl"
food[5] = "models/props_junk/garbage_metalcan001a.mdl"
food[6] = "models/props_lab/box01a.mdl"
food[7] = "models/props_lab/box01b.mdl"

    local frame = vgui.Create("DFrame")
local IconList = vgui.Create( "DPanelList", frame ) 

frame:Center()
frame:SetSize(220,200)
frame:SetTitle("Fridge")
frame:MakePopup()

IconList:EnableVerticalScrollbar( true ) 
IconList:EnableHorizontal( true ) 
IconList:SetPadding( 4 ) 
IconList:SetPos(10,30)
IconList:SetSize(200, 160)

for k,v in pairs(food) do
local icon = vgui.Create( "SpawnIcon", IconList ) 
icon:SetModel( v )
IconList:AddItem( icon )
icon.DoClick = function( icon ) surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("gm_spawn", v) end 
end 	

end

fridge();
[/lua]

[editline]09:30PM[/editline]

shared.lua
[lua]
DeriveGamemode(“sandbox”)
GM.Name = “Test” //Set the gamemode name
GM.Author = “Pdkm931” //Set the author name
GM.Email = “N/A” //Set the author email
GM.Website = “N/A” //Set the author website

team.SetUp( 1, “Guest”, Color( 125, 125, 125, 255 ) )

team.SetUp( 2, “Another Guest”, Color( 225, 225, 0 , 225 ) )

team.setUp( 3, “Warrior”, Color(225, 225, 20, 225))
[/lua]

[editline]09:31PM[/editline]

init.lua
[lua]
AddCSLuaFile( “cl_init.lua” ) //Tell the server that the client need to download cl_init.lua
AddCSLuaFile( “shared.lua” ) //Tell the server that the client need to download shared.lua

include( ‘shared.lua’ ) //Tell the server to load shared.lua

function GM:PlayerInitialSpawn( ply )

 ply:ConCommand( "team_menu" ) //Run the console command when the player first spawns 

end

function GM:PlayerLoadout(ply) --“The weapons/items that the player spawns with” function

ply:StripWeapons() -- This command strips all weapons from the player.

if ply:Team() == 1 then --If the player is on team "Guest"...
	ply:Give("weapon_357") -- ...then give them the Gravity Gun.
	

elseif ply:Team() == 2 then -- Otherwise, if the player is on team "Another Guest"...
	ply:Give("weapon_ar2") -- ...then give them the Phys Gun.
	ply:SetArmor( 25 )
	
	

elseif ply:Team() == 3 then
     ply:Give("weapon_crossbow")

end -- This ends the if/elseif.

end – This ends the function.

function team_1( ply )

 ply:SetTeam( 1 ) //Make the player join team 1 
 ply:Spawn()

end

function team_2( ply )

 ply:SetTeam( 2 ) //Make the player join team 2 
 ply:Spawn()

end

function team_3 ( ply )
ply:SetTeam( 3 )
ply:Spawn()
end

concommand.Add( “team_1”, team_1 ) //Add the command to set the players team to team 1
concommand.Add( “team_2”, team_2 ) //Add the command to set the players team to team 2
concommand.Add( “team_3”, team_3 )
concommand.Add( “gm_spawn”, gm_spawn v )

[/lua]

concommand.Add( “gm_spawn”, gm_spawn v )
clearly the problem, remove that line

Ok, ill test.

[editline]09:39PM[/editline]

Ok. it worked but now gmod dont give med weapons when I change team…

Learn 2 spell, you leave.


(User was banned for this post ("Why reply? / still not helpful" - mahalis))

Stupid 10er… iRzilla

Stupid 10er < iRzilla


(User was banned for this post ("Why reply / join-date elitism" - mahalis))

If the OP is still roaming here then you need to call the GAMEMODE:PlayerLoadout() from your GAMEMODE:PlayerSpawn() function.

Woah woah woah, where in any of those lua files does he include the init.lua?

You don’t need to.

People lied to me then, I feel betrayed.

Also it’s team.SetUp.

[lua]
team.SetUp( 1, “Guest”, Color( 125, 125, 125, 255 ) )
team.SetUp( 2, “Another Guest”, Color( 225, 225, 0 , 225 ) )
team.setUp( 3, “Warrior”, Color(225, 225, 20, 225))
[/lua]

I love how he told us all the errors in his console when he posted.

Oh, thanks. But what can I do to make the player only be able to spawn thigs from the fridge if he/she is on the “Guest” team? (I`ll make this team to engineer and the food types in the fridge to barriers)

Check if he has that object by looping through his “Fridge” table.