Spawn Menu

Hi guys, Ive started learning lua to make gmod gamemodes for me and my friends to play. Currently Im stuck with trying to make a derma spawn menu, I can get the menu to open with the buttons and everything but whenever I click the button I get a server error saying attempted to access local ‘ply’ a nil value.
cl_init.lua


include( "shared.lua" )

ply = FindMetaTable( "Player" )


function SpawnMenu ()

local playa = LocalPlayer()

local spawn_frame = vgui.Create("DFrame")
spawn_frame:SetSize(ScrW() * 0.317, ScrH() * 0.286)
spawn_frame:SetPos(ScrW() * 0.349, ScrH() * 0.4)
spawn_frame:SetTitle("Ready?")
spawn_frame:SetDeleteOnClose(false)
spawn_frame:ShowCloseButton(false)
spawn_frame:SetBackgroundBlur(true)
spawn_frame:SetDraggable(false)
spawn_frame:MakePopup()

local spec_btn = vgui.Create("DButton")
spec_btn:SetParent(spawn_frame)
spec_btn:SetSize(70, 25)
spec_btn:SetPos(285, 250)
spec_btn:SetText("Spectate")

spec_btn.DoClick = function()

	umsg.Start("spectate", ply)
	umsg.End()

 end

local spawn_btn = vgui.Create("DButton")
spawn_btn:SetParent(spawn_frame)
spawn_btn:SetSize(70, 25)
spawn_btn:SetPos(285, 215)
spawn_btn:SetText("Join...")

spawn_btn.DoClick = function()

	umsg.Start("spawn", ply)
	umsg.End()
	
 end

end

usermessage.Hook("spawn_vgui", SpawnMenu)

init.lua


AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include( "shared.lua" )

function GM:PlayerInitialSpawn( ply )
	
	ply:Spectate(5)
	
	umsg.Start("spawn_vgui", ply)
	umsg.End()
	
end

function spawnPlayer (ply)

		ply:Spawn()

end

function spectatePlayer (ply)

		ply:Spectate(5)

end

usermessage.Hook("spawn", spawnPlayer(ply))
usermessage.Hook("spectate", spectatePlayer(ply))

thanks for any help :slight_smile:

Hey there. The usermessage system is actually deprecated now, meaning you shouldn’t use it. Instead, look into the fancy “Net Library” system: http://wiki.garrysmod.com/page/Net_Library_Usage
I’ve rewrote your code converting said usermessages into net messages. I tried to put comments where I felt clarification was needed:

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

local function SpawnMenu()
local spawn_frame = vgui.Create(“DFrame”)
spawn_frame:SetSize(ScrW() * 0.317, ScrH() * 0.286)
spawn_frame:SetPos(ScrW() * 0.349, ScrH() * 0.4)
spawn_frame:SetTitle(“Ready?”)
spawn_frame:SetDeleteOnClose(false)
spawn_frame:ShowCloseButton(false)
spawn_frame:SetBackgroundBlur(true)
spawn_frame:SetDraggable(false)
spawn_frame:MakePopup()

local spec_btn = vgui.Create("DButton")
spec_btn:SetParent(spawn_frame)
spec_btn:SetSize(70, 25)
spec_btn:SetPos(285, 250)
spec_btn:SetText("Spectate")

spec_btn.DoClick = function()
	net.Start("mygamemode_spawn_response") -- we use netmessages because usermessages are deprecated (meant to not be used)
		net.WriteBit(0) -- we use 0 to represent spectating. Alternatively, we could send a string.
	net.SendToServer()
end

local spawn_btn = vgui.Create("DButton")
spawn_btn:SetParent(spawn_frame)
spawn_btn:SetSize(70, 25)
spawn_btn:SetPos(285, 215)
spawn_btn:SetText("Join...")

spawn_btn.DoClick = function()
	net.Start("mygamemode_spawn_response") -- we use netmessages because usermessages are deprecated (meant to not be used)
		net.WriteBit(1) -- we use 1 to represent spawning. Alternatively, we could send a string.
	net.SendToServer()
end

end

net.Receive(“mygamemode_spawn_vgui”, SpawnMenu)
[/lua]

init:
[lua]
AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“shared.lua”)

include(“shared.lua”)

util.AddNetworkString(“mygamemode_spawn_vgui”) – prepare the net messages
util.AddNetworkString(“mygamemode_spawn_response”)

function GM:PlayerInitialSpawn(ply)
ply:Spectate(5)

net.Start("mygamemode_spawn_vgui")
net.Send(ply)

end

local function SpawnOrSpectate(len, ply)
local response = net.ReadBit() – read the response we sent
if response == 1 then – spawning
ply:Spawn()
elseif response == 0 then – spectating
ply:Spectate(OBS_MODE_CHASE)
end
end

net.Receive(“mygamemode_spawn_response”, SpawnOrSpectate)
[/lua]

Also what text editor do you use to write code? You might want to look into either notepad++ or sublime text and the gmod lua plugins for each. It seems your spacing is inconsistent and what not.