The correct way to make an addon

What’s the correct way to make an addon?

I’ve created code within a gamemode and everything works. For some reason whenever I try to throw this into
addons/addonname/lua/autorun my code prints a shit ton of errors and is making me very frustrated. For example at the top of my code when ever I try to create a font, surface is returning nil
or if I create a variable such as local x = ScrW()
ScrW() is nil
or local ply = LocalPlayer()
LocalPlayer is nil

I tried to make two different “Menus” in one client side file. One menu will open, but the other won’t due to vgui.create returning Nil.
Menu 1 works, but Menu 2 doesn’t work.
But if I change Menu 1’s function name to Menu 2, that menu no longer works, but Menu 2, with Menu’s 1 name now works.

If I code this within a gamemode though everything is just find and dandy. Can someone please give me a run down on how to do this correctly?

Put in lua/autorun/client

[editline]8th May 2016[/editline]

You can’t use the surface library in autorun because autorun is shared - meaning it gets run on the client AND the server, but the surface library only exists clientside

The same goes for LocalPlayer() - it doesn’t exist serverside

[editline]8th May 2016[/editline]

And yeah, put serverside stuff in the server folder

then for server files throw in put in lua/autorun/server ?

[editline]8th May 2016[/editline]

Thanks MPan1 for the help. I’ve never been covered completely with the basics. I had to put in so much extra effort not being able to create variables and such. Thank you, thank you.

:snip: Thanks :smiley: But if you want to keep it in lua/autorun, you could make it work by doing this:

if SERVER then
-- code

if CLIENT then
-- code