Roblox Lua: From Beginner to Advanced in 30 Minutes!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to Roblox Studio the primary development environment creating Roblox games on opening Studio you'll see a range of tools that allow you to modify create and script game elements on the left you'll see we have this x to close the file as well on the right you have the Explorer which displays the hierarchy of all the elements in your game on the right again you'll find the properties panel which shows details of any selected item let's dive in in Roblox Studio you can create a script by clicking here or right clicking and clicking insert object then we're going to create a script then we're going to rename this by clicking double clicking and we're going to call this main main script you can adjust it here as well as clicking here in Lua the backbone of scripting variables store data so this is a variable local age equals 15. data types include numbers like this they include strings so local name equals your name they also include booleans to true or false so local is student equals true and then we can use the print statement to actually print the console what does that mean well we have a view tab up here and if we click on output it will open up our air window so we can see what our errors are when we play the game so let's make sure this is always open as a programmer now we can change print name and we can do comma name this is going to combine concatenate other words this name into this and then we can change this to print the age so age is age and then we can also do a print and then we can do is student and we can print the is student value here so now if we come along and click run you can see here we have three print statements here so you get name your name h15 and is student true so we're declaring three variables age name and is student and then we print their values using the print function you can right click on this window here and click clear output and it will clear and make us a fresh slate now let's talk about control structures which are important which are if else while for loose let's talk about those so let's do if age is less than 18 then we're going to print you are you cannot let's just say Drive let's say it's some country where you can't drive if you're under the age of 18 and then we could say else if they are older than 18 then we will say print you can drive okay this if statement is going to basically do that so it's going to check if they're under 18 it will say you cannot drive else it will print you can drive there's also a way you can use a counter so I could say local account equals a one and then we can do a while loop by doing while count is less than or equal to five two and then we can print the count and I'll explain this in a sec so count equals count plus one okay so this code right here this section the if statement which is right here this if statement it's going to check the condition 8 is less than 18. if true it's going to print you cannot drive otherwise it prints your can drive the while loop then prints account and it prints one through five functions in Lua allow us to group code into reusable blocks we can use a function by doing function read name okay then we can return the function by doing hello or hello and then we can do this this will add this all together so these two dots is going to be a way to concatenate as well just like these commas here but what's this going to just be a different way if we had a certain if we had two of them so we couldn't we couldn't do commas like this it just doesn't look as clean so I like just use dots um as well for concatenation if it's like this and if it's like one thing then we'll just use the comma to make it look a little bit more clean so we can come back to our code later and then to actually print this and return the message we'll do local message equals and then we're going to call the function by doing greet and then we're going to put the name in here so I could say whatever name like my name is my name is Owen so I'll just put my name okay boom and now we can print the message to the output window now if we click run it actually comment out all this code so the comment I like to like select everything so select it all and then hold Ctrl command on that and then slash it's going to make all of the comments and then if you want to undo that we can just do this Ctrl slash and slash control slash and we'll just do that for now now we can click run now if we look in the output window you'll see Ola Owen so it looks like it's going to print this the output window so we defined a function called greet and that takes a parameter called name what is a parameter a parameter is a way to tell the code tell the computer what we're trying to return so let's say this is the name this is the name that we need to return later on so we're going to return the greeting message then we we then call this message function with the argument Owen and then print the result that's common this out control slash and now we're going to talk about events clearly output events in Roblox trigger on a specific action hacks for example I could get the player's service by doing local players equals a game colon get service players what this is going to do is going to get the player's service this is the Players service right here which is used for all what the players are stored when they join the game then we're going to do players.player added connect function then we're going to parentheses here and then we're going to put our player parameter inside of these parentheses and then we're going to print player dot name now what it's going to do is going to print this player's name so this is a way to get the player name that joined the game alright so now we're going to dot dot to concatenate once again and then we're gonna do quotes and then we're gonna do has joined the game now this is going to print when we join so when we click run you'll see I need to actually play go back run and play instead that's going to print stay sale has joined the game right here I don't know why it's printing these wires I think I need to disable this plug-in real quick if you always have if you ever have plugins you can disable go to here and just boom okay boom clear this output let's check that again alrighty stay so has joined the game as you can see it's going to print CSL has joined the game right so what does this code do exactly well this code listens for the player added event which is this event right here and then when a player joins is going to print a welcome message I just showed you in the output window now it's comment this out Roblox games consist of various objects like Parts models and tools let's create a simple part programmatically so instead of inserting a part by going to base plate and clicking on Parts this we can just go to test I mean go back to our script and we can type local heart okay I'm gonna do equals instance dot new now this is a way to insert any object into the game so for example we could do a local part also we can change each parts size Eagles and we can do Vector three which is a way to check size on parts we can set it to a hundred by a hundred by a hundred which is going to be around 100 100 100 technically this size okay now it's going to spawn at the orientation of zero zero zero as well as the position of zero zero zero so let's just make an example of how where it's going to spawn when it first spawns so what other properties can we change on this part we can go to transparency reflectance so five do that in the script so heart dot transparency equals 0.5 0.5 what else can we change brick color so it's changing royalty red do that we'll do part dot brick alert equals rate color dot new really red and we can also do anchor which is a way to basically mean that it's going to be not movable so when this game plays we'll see I'll show you guys will create what this does when it's on anchored as you can see it doesn't even show up it flings somewhere and if we anchor it we'll stay in place as you can see right here so that's how you would use that okay so yeah that's the that the way to do that is do part dot anchored and then make it says to a Boolean by doing true and then to put it inside of the actual workspace we have to do a parent technique so let's do heart dot parent which is going to put it inside of a different Place equals game dot workspace or we can do just workspace because that's the new thing okay so workspace in this example we're creating a new part setting its size color and ensuring an anchor it is while it's heading to transparency and then we're adding it to the workspace the comments now every object in Roblox has properties these Define the object's behavior and appearance let's manipulate some properties let's do local heart equals game Dot workspace.part and workspace and game.orgspace the same exact thing you just need it doesn't really matter what you do just stick to one thing if you once but it doesn't only matter it's the same exact mechanic also do heart.transparency equals only five then we can print part dot size okay minus one deal when we print we run going to print the size here so it's going to be 100 by 100 by a hundred already so in this part section we first access of heart in the workspace which is this part right here that's really red then we said the transparency property and print its size the output there's also a way to create guis so let's create a GUI by doing a screen GUI and then adding a frame in here and then also adding a making this full screen by doing the size one zero one zero that make it fill the whole screen then we can change the star okay now that we have a frame we're going to add a uh image bucket then we're going to censor this or just try because I'm just a coding tutorial okay there we go no I can't answer this in the frame we can call this button let's say play all right now this is my this might be like a menu screen for example we got to create a local script inside of here we'll name this script click script just as an example so I get this through alrighty now you gotta actually get the actual button so how would that we'll do local button equals script.parent whereas when he was going to look for the button but script means it's going to look at this script so the hierarchy of the script so what does parent mean well parents the opposite so we're going to go up one so if I went up once that would be play but if I wanted twice I'd do script.parent.parent uh now I go up to the frame and also if I go to short parent again it's going to go up to the screen GUI parent again let's go up to the starter DUI and so on and so forth so it's going to keep on going up now how would we actually click this let's remove the other code here descriptive apparent because it's the parent now we can do button dot mouse button one click connect function what this is going to do is it's going to when the player clicks this button we're going to then run code so I could say print button was click okay now now we create a screen GUI by we create a screen GUI and then we added an image button inside of it when the button is clicked button was clicked will be printed so it's gone actually no not run play there we go open up the output window click it but it was click button click almost Click Boom alrighty now we're going back to our main script where to go and comment this out animating objects adds Dynamics and animating objects as Dynamics to a game Let's script up heart and move it up and down by doing local part equals workspace.heart then we're gonna do 4i equals 110. this is going to go through the loop 10 times then we're gonna do part dot position equals part.position which is going to send it to each other so we can set the value change it then we're going to do e plus Vector 3. new one zero zero then we're going to task dot weight one or let's say 1.5 because it's a spinning part then we're gonna do a same thing so we'll do this and then we're going to minus it so minus then we're going to copy this so you should type all this um if you're a beginner because this will really help you learn how to code and but for the more advanced people I'd suggest saving time a little bit and copying the same exact handic but it will help you remember what you're doing all right so this part moves up by one stud waits for 0.5 seconds and then it moves down one step repeating this ten times so if we play We disable the tree UI by enabling this oh let me play so you can see it's raising that's raising a little bit not too much and you can see it's raising about one stud and moving it down we can change this to be more subtle but that's all that's what the example is how you can use for Loops in the most basic manner interactivity is key in games with click and touch detectors and objects can respond to player actions so if we were to clear eight parts right here to put it right here then we were gonna put a click detector inside of it so clicked editor then we can see map activation distance now we can create a script inside of here then we can do part local part equals workspace Dot and we'll change this part name to click part we'll do workspace and then we're gonna do local detector equals script dot uh script uh parent then we're gonna do the Tedder dot mouse click which is going to detect if they click on that part then we're going to do connect function player okay this is going to connect the function to this event here then we're going to print player.name it's going to get the player's name again and we'll do the part okay that's what we do there and this is going to click this is going to print when we actually click the part so let's open up Roblox Studio play now we're going to go down to our parts and as you can see there's a there's a click there's a little click icon here click to stay so click the part we'll get as many times as you want still prints the object so you would code something with a click event now that you know all the basics we're gonna go into more advanced Lua but let's first get into intermediate talks so first of all Luba tables are versatile structures allowing list dictionaries and more combined with meta tables we can manipulate their default behavior let's explore these Concepts but first let's actually explain what meta tables are let's open up our new let's make actually create a new script let's create a new script in this service or service and call this intermediate so intermediate okay now we're going to first talk about tables so create a table called local fruits equals banana comma Cherry okay then we can print fruits and what one this will do is it will print all the fruit so as you can see prints the whole table inside of here okay but how would we print one well we could print one of them by doing inside the table we could do two this is gonna it's going to print banana and so on so whatever the number of the table is we can click run it's going to print Sherry so this is going to print this Cherry here but we can change this to whatever number we like that's in this table array now we'll change the actual colors so let's do another table called fruit colors equals then we can do Apple equals red whatever color they are correspondence do banana equals yellow and we could also do cherry equals red all right now after we do this we're going to set a meta table now what does that even mean and why would you use a meta table well you can use the meta table we can set a default Behavior where the key is not found in fruit colors so this is an easy way to check if the key is not found so let's do set meta table fruit colors and then we're going to do comma and then we're gonna do a bracket here and then we're going to do this is just some basic syntax of Lua right here okay then we're gonna do underscore underscore index equal to function table key and then we're going to return unknown okay and now this med table is going to print when if there's not a if there's an unknown color so for example if we print fruit colors.org and then we click play by clicking on run the window this is unknown color okay so what this is doing is it's checking if color exists if it doesn't then it's going to print unknown this is an easy way to check if something exists or not in a table all right now if it changes to Red let me click around it's going to print unknown color so we gotta do apple and then it will say red as you can see right here so it's going to print the correct color or spawning with the fruit so if we put Apple here banana or cherry it would say it was existing otherwise it would return this unknown color so in this we Define a list fruits and a dictionary fruit colors using meta tables we set the default behavior when a key is not found for the fruit colors then we print the output of the window this is just an example now it's cutting this out Luba supports ooh Lua supports object oriented programming although known as oop through meta tables let's create a simple class representing a car with methods to Showcase this so let's create a local car class and we're gonna like I said this to an MD class and then we're going to do car dot underscore underscore index equals car and we're going to put this to a Constructor here so what does that mean well it's going to set this to the car then we're going to create a function called function car dot new and then we're going to put our model in here then we're gonna do local self and then we're going to do equals set meta table and then we're going to do a empty class in here as well as a car now this is going to set the index to the car so this is going to set this index to the cart and then we're creating a new function that's going to be a method of this new element here then we're going to put the car model inside of here as a parameter then we're going to define the local self you may wondering what does self mean well self represents the instance of the object itself it's commonly used in methods to access properties and methods within the same object so this is the same object we wanted to access this we'd use this cell method if you have an example of if you have a object representing car and the car has properties like color and methods to drive and the methods of this car object you use self to refer to the specific instances of the car you're working with so this is easy to change properties of the car from Roblox so now we're going to set himself.model equals model so the model to the model then we're going to return self okay it's going to return this to this code here so that's what this function will do we'll get both these if they exist and then return it to the out to the output or either the actual game code base then we're going to do a function car dot colon Drive remember to do print self dot model and we're going to dot dot is deriving it's gonna check if they're driving and then we can actually use this by doing local my car equals car dot new Toyota okay it's a brand new car called Toyota and then we're going to set that to my car colon Drive okay this is the basics of object oriented programming next we're going to talk about player and character scripting how to script player and character behaviors it's a vital for custom behaviors let's make a players character jump when they chat so let's do this by doing logame.players.player added connect function player and then we're gonna do player.chatted so it's the way to check when they chat and it's in the chat system connect function in message okay then we're gonna do if message is equal equal to jump then and player dot character player dot character exists in the game character is the model and the side of the workspace if that exists then we're gonna do player dot character.humanoid.jump equals true now play this game real quick go to play then we chat jump we just now I can jump every time I say jump in the chat so we listen for players joining then for the chat messages they chat jump we make the character jump sound enhances game atmosphere let's script a button that plays the sound effect once being clicked so do this we'll create a new we'll make this spot this screened UI visible again and we'll edit our script by commenting this out and then we'll also do a local sound equals instance dot new sound it's going to create a new sound we'll set the sound dot sound ID to whatever sound ID we need that's on the Roblox website so the ID is going to be the website link so you'll just copy the ID that whatever the sound you find on the Roblox website is for example it could be one two three four five six seven eight nine ten okay now now as I said the sound.harrant we'll do sound dot parent equals game workspace then we're gonna do find the button by doing local button equals script.parent and then we're going to do a button dot mouse button one-click connect function and then we're going to put sound colon play what it's going to do is going to play the sound when they click the button so this is a sound ID we could play and we would hear the sound so when I click this it would play but this says fail to load sound because it doesn't exist but if you were to click this it would actually play this sound and then we can also wait a couple seconds before removing the sound so it doesn't make a bunch of sound so tasked out wait 0.5 and then we could do sound colon destroy or remove I always use destroy because I'm used to it but you can use whatever you'd like that's going to destroy the sound so then when every time they click it it's going to create a new sound okay so it's not going to span the output with I mean the game with sounds and maybe make it lag but a sound is created and parented to the workspace as you can see right here and then when a button in the workspace is clicked I mean the starter guis click the sound place now let's talk about sending information either Network in Roblox so it's essential for multiplayer games we can send a message from the client to the server using a remote event let's do a local event equals let me create an event replicated storage call it records game.replicatedstorage.remote event then we can do event dot actually move this to a server script real quick let's do this server script so we need to disable this real quick and then we need to make sure this event's fired and now we can go to our main our intermediate script scroll down and say this event dot on server event connect function player comma message okay it's going to return two parameters then we're gonna print player dot name dot dot said we do dot dot message okay this is going to be an easy way for us to remote events okay now we're gonna go back to our client script and then we're going to call the remote event again but we're going to say event colon fire server hello server now scoring is going to basically fire this from the client to the server so the server is going to listen for the remote event right here when the client fires the event the server prints the received message so we click play DSL said hello server now we can add this into anything like for example when a button is clicked we could fire the server so instead of doing this we'll just do button do script.parent dot mouse button one click connect function then we can fire the server when it's only so let's do that remove this and when we click it will not print until we click it so that's gonna click we will event fire the event alrighty that's how you would use events now once disable our enable screen our GUI now we're gonna go over modules so modules are different than scripts so you create a module by clicking module script and what is a module local module is going to Define what our code is running so I'm going to compare two things okay so let's just compare a module script versus a script so let's create a function module dot add a b so it's going to basically add this function to the module then we're going to return a plus b okay then we're going to return the module down here now this is the module script if we wanted to create a new script because this is more advanced topic we'll do normal and then we'll just we'll just say what what this same thing would do in the actual script so let's do local math module equals and then we'll require game.serverscriptservice.math um let's say module script your name is to math actually math okay more descriptive now this is going to grab the module math just what we just made right here and then to actually reference this we'll actually do a print math module or math.ad map actually sorry math module.add and we can add our our things here so five and three so now we click play I'm going to put eight over here so you can use any numbers and you can also change this around change this to minus as well so minus b as well as you know anything you'd like so that's how you would do that all right so that's how modules work Beyond Roblox is built-in events you can create custom events these allow for specific interactions between scripts so let's explore this let's comment this out as well as create a local custom event equals replicates oh let's say instance dot new bindable event then we're gonna do custom event dot event connect function message print received message and then we'll put the message at the end so comma message then we're going to fire the event by doing custom event colon fire hello custom event so we create a bindable event can I do a function to it and then we fire the event and with a message so when fired the connected function it runs and prints the message so quickly receive message hello custom event alrighty now let's go over Advanced animations animating characters and objects brings life to your game let's look at how to use the animation controller for advanced animations this is a conceptual example actual if a meditation would evolve more setup in an animation asset already created but I'll show you the basics of how to use animation so let's put it in our local secure let's do local humanoid equals game dot players.localplayer.character.humanoid okay and then this is going to get the characters humanoid inside of their health that's where their health is stored all their stats and stuff then we're going to create a local animation equals instance dot new and then we're going to call this animation okay and then we do animation dot animation ID equals and then we're going to RBX asset ID and then we're going to put our animation ID right here that we uploaded on Roblox I'll use a zero zero zero for now as an example then to load the animation with the local track equals humanoid Coleman load animation they'll put animation in here then I'm going to do track hole and play okay what it's going to do is going to look for the track and then play it which is going to play the animation so we're creating a new animation object right here and we're setting as animation ID which is crucial we're loading it onto the humanoid and playing it now let's go over pathfinding as an example so let's do local pathfinding service equals game colon get service pathfinding service then we're gonna do the local NPC that we want to have pathfinding happen to so if we had an NBC let's say in the workspace we would do local NBC equals game dot workspace.mpc then that's gonna be a model so then we would do local Target position equals and then we'll set this position to Vector three which is the size and proposition oh we'll set it to 50 zero boom now what location is that on the map well if we set this to a position to zero what is it 50 0 50 it'll move right here so this is where the this is where the character will move on the map so they would spawn here I guess and then when they would move right over here now I'll upgrade path by doing local path equals path finding service colon create then we're going to set the agent radius equals 2 and then we're going to set the agent height equals five then we're going to set the agent can jump equals true then we'll say agent jump height equals 10 and then we're going to say agent Max slope equals 40 bucks just some parameters we can use I know it's a lot of stuff here but we can just make this more clean by just click enter and basically just making each line a new property here so there we have it there's our path and now we can actually create the path by doing path colon commute async NPC dot position comma Target position this is going to start half from start to finish then we're going to do if path dot status is equal equal to enum dot hathstatus dot complete then we're going to do path colon move to which is going to move the player or NBC to the NBC so I want to put this in the parameter here so we're going to create a pathfinding path for NPC here and then we're going to path if a path calculation is successful NPC starts moving towards Target right here okay so this is going to move towards Target let's talk about Ai and NPC Behavior and scripting complex NPC behaviors so if we were to create an NPC let's say we created local NPC equals game.workspace.mpc once again I already put it up there I just want to make an example so now we're going to do local player equals game.players.localplayer okay then we're gonna do game dot workspace dot current camera get property change signal C frame I'm just going to check if the property has changed then we'll do colon connect function okay then we're going to declare a variable called local distance to check how far they are for way from the NBC so do this we'll do player.character.humanoid root part dot position okay then we're gonna do minus NPC dot position and then we'll do dot magnitude outside of these then this what is it going to do is going to check how close the distance is from the player and the NBC then we're going to do if distance and distance distance is less than 10 then they're closer than 10 studs then the C frame axis we're going to run this code if they are close to follow player then we do else we're gonna do code to make NPC idle okay this is an example also our next topic is security you never want to trust the client always validate on the server so for example if we this is a bad example so bad example we'll do this is a bad example so don't never do this so local player money equals game dot players.players.localplayer.money.value never do this because it is vulnerable to its client-sided exploits a good example is to reuse remote functions or remote events to securely communicate between client and server and validate all changes on the server side so the first approach directly accesses a client value which can be exploited the second emphasize is validating everything on the server so instead of doing this we would do it on the server side and use a remote event every time something's changed or something needs to be changed that is a great idea because then people can't exploit the system and get infinity or I mean the server is always more trusted than the client that is pretty much it for this free tutorial if you want to learn more go to quizgrid.com enroll in the complete course and you'll be mastering your scripting in no time thank you for watching I will see you in the next video have a great one
Info
Channel: TheRobloxCoach
Views: 11,328
Rating: undefined out of 5
Keywords: programming, programming roblox, coding, roblox studio, lua, roblox lua, game development, roblox scripting tutorial, scripting, roblox, lua tutorial, how to script on roblox, chatgpt, gamedev, game dev, roblox studio tutorial, chatgpt roblox, roblox advanced scripting tutorial, how to easily script, roblox object oriented programming, roblox script, roblox studio scripting, developing on roblox easily, script on roblox, script on roblox studio, scripting on roblox free
Id: tb0lSErUtMI
Channel Id: undefined
Length: 25min 54sec (1554 seconds)
Published: Thu Sep 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.