Okay, I’ve been trying to learn how to code Lua a lot lately, and I can understand how other people’s coding works pretty well. But whenever I start with a blank page with notepad, I am COMPLETELY lost… So, do any of you Lua kings know how I could stop this problem? Help is extremely appreciated.
I would be more than happy to help you considering you aren’t trying to “LUA”.
So when you open up Notepad, you’re lost because you don’t know where to go. People don’t just start coding out of nothing, you have to have an idea. Most of the coding I’ve done was just the start of some idea. For example, let’s say you wanted to make it so that whenever you die everyone hears a funeral march.
First, you would want to find the sound you want to play and stick it in your sound folder. Create a variable and precache the sound.
Second, you want to figure out how to play a sound. The GMod Wiki is incredibly helpful. So in this case, you could use player:EmitSound() or something.
Third, you want to figure out how to run this function when someone dies. In this case, you’re looking at gamemode hooks, so you could check that section of the wiki. The hook is PlayerDeath.
So you have all the info, now put it together. You make a function for your hook, and simply put a player:EmitSound(sound) inside, and use hook.Add to finish it up.
However, you’re not done yet. A Lua file in the middle of nowhere does no one any good; you want to know where to put it. In this case, you want to put the file in your /lua/autorun/server, because adding something to PlayerDeath requires a serverside function.
And viola! You’ve coded in Lua. So, tl;dr:
Know where you want to go before you start.
As Entoros stated, you’ll need an idea from which to code. However once you have that idea, looking at a blank page can be quite intimidating, especially if your idea is ambitious. However, the best way to get along is to write down all your ideas on paper, a notepad, a word document… Whatever you want, and get them organized. This makes coding things much easier. When I go to code, I usually put things in order of priority they need done, and jot down some ideas I have on how to do them. If they involve fairly complicated structures I map them out as well. Still, looking at that blank page is pretty scary, so I just code one feature at a time, and before you know it, you’ve got something.
Thanks, this helps a lot.
But is this how all Lua coding is done?
What I really want to do is make my own gamemode, or at least learn more from attempting.
Uhm… we can’t really tell you how to do everything in Lua, otherwise we might as well code it for you. However, you can learn from their official website, google it. Or you can read through TH89’s tutorial. That’s what helped me when I initially started Lua, however I had experience with Java, and the Object Oriented Theory, so it wasn’t too hard to pick up.
If you want to make your own gamemode… that’s the most involved thing you can do GMod Lua wise. Your going to want to start with some simple SWEPS or Addons. Probably not ones you’ll want to release either. People hate “5 second SWEPS” here. However, it’s okay to make them to experiment around, but just make sure you evolve out of that.
Once you read through a few of the tutorials, just try to make something… Don’t come here for every error, part of Lua is getting an error, and solving it. Don’t be afraid to make something and have it probably be horribly broken the first time. Nothing ever works as intended on the first run… Keep at it.
For some gamemode help (I’ve never personally coded one), I would look here:
Thank you, this is exactly what I needed. Learning by the second.