how to write CLEANER code - Roblox Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you haven't seen it already there's this game on Roblox floating around on the recommended page called Script box and according to the description script box is a place to find scripts to use for your game the scripts are free to use in any Roblox game with no credit required these scripts are not to be used for commissions or any other kind of paid work all scripts in the game are written by purple Lua and contributors so when I first saw this I decided to hop into the game to see the quality of the example scripts and I must say there are a lot of things that can be improved so I thought this would be a great learning opportunity for new scriptors to see how code can be improved and we're going to transform some of the example code given in this game to follow more closely to the official louu style guide and just have some general improvements in the logic of the code so this is the page of the game that shows a bunch of free scripts that you can use for your games and I was looking through several of these scripts and I noticed there was a lot of things that could be changed or improved in the logic of the code for example let's just hop into to some random one here I don't know let's pick uh let's go ahead and pick a land mine right let's take a look at this script it says to put this code in a server script that is in the part you want to explode and it has a respawn time and when the parent is touched if the transparency is equal to one it's going to return otherwise it sets it to one which is good that means you have a debounce here and then it creates a new explosion and it sets the position to be the position of this part and then adds it to the debris service and then yields using the deprecated weight function and then it sets the transparency back to zero now this works fine but there are a couple things to note first off explosions are automatically cleaned up when you parent them to the workpace so there's actually no need to use the debris service and this weight function is deprecated so you should use task. weight instead of this older weight function along with that if you want to follow the Roblox louu Style guide then having these on line if statements is actually kind of an isore to read in your code and it's better for you to just you know move this code down and indent the return statement and have it be in its own block instead of having this one long if statement that's mostly preference but for the readability sake of your code you're probably going to want to move the return statement down and this is just one random example in the code I was looking through this with another developer buddy and we kind of thought that maybe this guy just used chat GPT to generate the code and then he just decided oh you know what I'll just make a bunch of money off of this AI generated code which might be the case but I don't think that's likely let's go ahead and pick another set of code here let's try the badge door right so I'm just going to copy this code real quick I'm not going to say anything yet we're going to move into studio and we're going to take a look at how we can improve it so specifically with this code it wants us to put this code inside of a script which I did but the script also needs to be a child of some kind of part we want to act as a door so let me just make a apart here real quick I'll just make something really simple do something like that and uh I'll make it red or something and then I'll make sure to Anchor that and we'll move the script inside of the part and actually we should just test to see if the code is even working that's always a very important step to see if this code's even working at all if we touch it okay perfect it kills us which is great now just taking a look at this code I'm going to give you a few seconds to figure out how you can improve it and there's definitely several are in the script that we can improve and hopefully you'll catch what I'm talking about so the first thing I would change is to make sure that your code is using some kind of consistent naming scheme I noticed in other scripts he was using the camel case naming convention specifically with this script he's using snake case instead and it's just generally not a good practice to use different naming conventions for variables in your game because it can get kind of confusing so I'm just going to quickly rename this to badge ID now if you wanted this badge ID to also act as a constant then you could do something like capitalize snake case that would work fine as well but the next thing I want to go ahead and change is of course moving this return statement to be within its own scope indented this is how your return statement should look yes even if it's just a simple kind of check statement that will return out of the function if some condition isn't met you still want to do it this way and that's even recommended by the official louu style guide the next thing that needs to improve is that we're calling an asynchronous function here without wrapping it in a PE call what I thought was weird is that we're wrapping this code in a PE call but we're not actually wrapping the code that needs to be wrapped in a PE call so let's actually go ahead and do that we'll get success and results and we'll create a PE call here and we'll just copy this code and we're going to return it and if we were not successful or the player does not have this badge then we need to kill them and another thing we need to change here is that well obviously when you're going to be naming variables don't name them with one letter that does not help anybody who's reading your code you should give your variables descriptive names so you know exactly what the heck they are especially for other people are going to be reading your code I really hate it when people abbreviate or just use single letters to name things in their code it it's not readable at all another thing to note is that you don't even need to wrap this in a PE call we're just setting the humanoids Health to zero so instead we can just delete this and since we already have a reference to the player we can just refer to player. character and get their humanoid and just set their health equal to zero so now if we go and touch our door we're going to have the same effect we die but the code is much cleaner to read and we weren't wrapping a PE call in some code that didn't need a PE call and we actually wrapped the code that needed a PE call with a PE call now if you wanted to be even extra safe then you can make a variable for your humanoid and you do player. character F first child which is a humanoid and and then you can check to see if there's a humanoid and whatnot you don't need to check if there's a character because we got hit. parent here which returned us the player if hit. parent wasn't the player's character it wouldn't have returned us the player so we don't need to check if the character exists we can assume it already exists but we don't know about the humanoid if we just want to double check then if there is a humanoid we can set the character Health to zero we can also improve this next piece of code right here I'm going to move it into Studio and it might not be clear right away how exactly we could improve this code but I will show you in a second and some of you more experienced developers might see how we can improve this code so to actually improve this code what we want to do is we actually want to create two dedicated functions one to listen for when a character is added and one to listen for when a humanoid dies so we can create a function and we're going to call this on character added and we're going to have another function and we're going to call this on humanoid death now instead of connecting a new Lambda function here every single time a player joins the game instead we can just simply pass a reference to our on character added function instead to handle the functionality that we want whenever a character is added to the player the reason why this is better is because we're using less memory we're not having to create a new Lambda function every single time this code runs we're always going to refer back to the same function and saving memory is always a good thing when you're making games so we can just copy this functionality here I'm going to migrate it and place it in here and this function is going to be passed a character model and all we need to do is just pass or connect to the on character added function now the next interesting thing to note is that we don't need to use wait for child here this is going to be in a server script and there's no point to use wait for child the humanoids already going to be accessible so we can just directly index for it using the dot notation and then the next thing we can do here is connect our on humanoid death function to go ahead and reload the character when their humanoid dies now you're going to see some issues wait how is the onh humanoid death function going to know which player died and this is where you're going to have to make a little bit of adjustment so for example what we could do here is we could keep the lamb the function but we're still going to make a reference to our onh humanoid death function that way we don't have to keep creating new functions or new code every single time a character is added we just simply call our onh humanoid death function which just it's a reference to onh humanoid death so we're still going to save memory that way this function is going to need to know the player that died so the easiest way that we could do it is we could just simply refer to the player service and get the player from the character and since we're already using the player service down here it's going to make more sense to go ahead and store inside of a variable so we'll make a variable called players and that's going to be equal to game get service players and then when a humanoid dies in a character then we can pass to our onh humanoid death function the player which is going to be players get player from character and we can go ahead and pass our character here and then finally inside of our onh humanoid death function we can just refer to our player and load the character now if we go and reset our character we're going to instantly respond just like how we would expect but the great thing about it is that we are saving on memory by connecting to the on character added event with our already pre-made on character added function there's no need to keep creating new Lambda functions every single time a player joins the game and we save on memory and then of course another great benefit with the structuring of our code is that it's a lot more organized so in any case we wanted to add more stuff in the future maybe we wanted to add more functionality to our on character added function or maybe we wanted to add more functionality to our onh humanoid death function we can easily do so Within These functions and it's clear what each of these functions are supposed to do let's take a look at another script inside of here um I don't know how about random sounds play random sounds at random intervals good for horror games put this code in a local script under starter player scripts put all the sound effects you want to play inside of the script and then here's our code so let's actually go ahead and copy this and let's see how we can improve it so we need to make a new local script in starter player scripts I'm just going to call this my random sound Handler and let's put our code inside of there and then we need to add some sounds as children of the script let's just hop into the toolbox and grab some real quick okay so I just grabbed some random sounds from the toolbox that we're going to be randomly playing with our random sound Handler so let's actually just keep the code how it is right now and see if it works and we'll sit here and wait and if any sound will play okay there we go we got uh one of the sounds to play let's see if we can get another okay yeah there we go so the script is indeed working but there are ways we can improve it and I'm going to give you a couple seconds to see if you can figure out how well the first way that we can improve it is by not using the deprecated weight function and using task. weight instead it's just better use it the next thing we can do is we can improve the random number generator by setting a random seed So math. Random seed we can set a seed for the randomc generator to make the random number generator a little bit more random so the most common way Roblox developers do it is that they just pass or call The Tick function here which will return a number and that number is going to act as the seed so every single game a player joins the seed is always going to be different so by passing The Tick here we get a different random number generator the next thing we can improve here is the script get children function call because we're calling it twice here which isn't necessary we can just make a variable and we'll call this sounds and that's going to be equal to script get children and now we can refer to our sounds array and then we can pick a random sound of there from one to the length of our sounds array and then we can go ahead and play that sound and if we play test our game we'll wait a few seconds here and hopefully we'll get some kind of horror sound to play okay there we go we got one horror sound let's see if we can get another one all right we got the same one again but that's fine at least it is working now another way we could further improve this is that we could add another yield statement to wait for the sound that we have chosen to finish playing because you might play multiple sounds on top of each other especially if one of these sounds exceeds the time of 5 Seconds so if any sound exceeds 5 seconds in duration yet when we yield here we do another 5 Seconds of waiting and then we play another sound you might have sounds overlapping each other and that might not be what you want so let's actually go ahead and make a variable here we're going to call this random index and that's going to be equal to our math. random function call so then we can go ahead and refer to our sounds and get the random index and play that sound and then afterwards we can do the exact same thing again by using a random index and we can go ahead and wait for when that sound ends so I believe the event is ended let me actually type annotate this as an array that contains sound so I just make sure the event is actually ended okay yeah ended fires when the sound has completely stopped playback so we'll wait wait for that to Fire and now we are guaranteed to play a new sound and no sounds are going to overlap each other we're going to wait for the current sound that is playing to end now that was just a few of the scripts in this game there are a ton more in here that can definitely be improved but another way this game can be improved is that the descriptions can help tell the players or new scriptors what exactly the script is doing because the only instructions in here is just telling you where to put the code and that's it and then it works but you don't know why it works I feel like it would be useful if there was at least comments in the code or something some kind of description somewhere that explains what exactly is going on not just how the code works but also why the code works and the decision- making of the developer who wrote this code for any of you new scripts out there I actually think this game is going to serve as a really good learning opportunity for you to go into some of these scripts and actually improve it using some of the techniques that we saw here in this video and I also hope this video has improved or sharpened your scripting scale and you're now feeling more confident with writing cleaner code otherwise thank you for watching and I will see you next time
Info
Channel: Crusherfire
Views: 8,539
Rating: undefined out of 5
Keywords:
Id: NsPL5yDY5cE
Channel Id: undefined
Length: 14min 36sec (876 seconds)
Published: Wed May 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.