Menu won't open on player initialize.

My script is simple but causes this error


attempt to index local 'main' (a nil value)

My code is


if SERVER then
util.AddNetworkString("openmenu")

hook.Add("PlayerInitialSpawn","MGLUA_PlayerInitialSpawn_Identifier",function(ply)
	net.Start("openmenu")
	net.Send(ply)
end)
end

if CLIENT then
net.Receive("openmenu",function()
	local main = vgui.Create("DFrame")
	main:SetSize(1400,400)
	main:SetTitle("")
	main:Center()
end)
end

If this is not possible using this method, please let me know if there is another way to open a menu when a player spawns.

You switched the first and second argument for the hook. The first one should be PlayerInitialSpawn. And the second should be the identifier.

I switched them around but I still got this problem, atleast the same error.

That may be due to the player not fully initializing and not being able to create the vgui menu.
I’d recommend perhaps putting this code clientside instead on maybe the Initialize hook and using a timer. Or check when LocalPlayer is valid and create the menu then if possible.