welcome back to my Roblox beginner scripting tutorial series my name is balev and if you've made it this far into the series well first of all shout out to you for making it this far but second I have good news for you this is the final New Concept we're going to be learning inside of this tutorial guide and I'm going to go more into detail about this later but essentially in the next episode after this one we're going to be taking everything we've learned and putting it all into our first game that we're going to be creating and before we can do that I need to teach you this last concept which argu is the hardest one in the series and it's also going to be the longest one in the series so this one's going to require your full attention and I hope that you're going to be able to understand this concept by the end of this episode so what are we going to be learning for this final concept well I'm going to be teaching you tables and how to use them inside of your Roblox games so what is a table it's basically a data structure that holds multiple pieces of data together all inside of one sort of collection so it's when you take multiple pieces of data with different data types you put them together so that you can manipulate them or move them around or even access them as you so desire to all contained within one collection or one table or one group or however you want to call it inside of your script and there's two types of tables that I'm going to be teaching you inside of this video one of them is arrays and the second one is dictionaries so if we were to go to the Explorer and then we inserted a script inside the workspace I'm going to rename the script to tables and I'm going to hit enter just like this and I'm also going to delete the code here okay so the first one I'm going to be teaching you is arrays now basically what an array is is it's a table where we group values together in sequential order and it's just going to be values that we're going to be storing inside of a table and that's all it is going to be so if I were to show you what this looks like let's first create a variable so I'm just going to call this one local my array just like this and then we're going to set this equal to a new syntax that I haven't showed you before because this one is going to be specifically for creating tables so what we're going to do is write down the open and close curly brackets just like this so we're not going to be using open close parentheses like we have been in a lot of the tutorials in the past but instead we're going to be using open and close curly brackets to indicate that this is going to be a new table that we're going to create and right now this table is empty because we haven't put values inside of it so let me show you how to add values inside of this array so what we can do is basically put down whatever value we want and this can be of any type it can be a number it can be a string just like this or it could even be a Boolean as well it doesn't really matter what it is but let's say for our first value we put inside of here it can be a number 10 just like this now if we want to add another value to this we separate each value we add with a comma just like this and then we add something new like a string uh we can do the same thing with adding a Boolean and we could probably add another number as well like I don't know 500 or something and so now we have four values inside of this table now what's interesting about an array is first of all we're just adding values inside of this table but second the way we access values is also in sequential order from the left side all the way to the right side so it's important to know how to access values inside of an array and that's by using their positional value of where they're located inside the table now what do I mean by this well if we started at 10 this is essentially at position one inside of the table and if we were at the string value it's at position two inside of the array this one's at position three and then this one's at position four so it goes from left to right just like this now if we to access a specific value inside of this array then we would drop a line just like this we'll make a print statement and then what we're going to do is let's say we want to access the first value inside of here um then what we're going to do is first write down the array just like this say my array and then we're going to add in open and close hard brackets just like this so it kind of looks like a square just like this um because what we're going to put inside of here is a positional value that's going to essentially look through the table find the value that's inside the position and then it's going to return it so in this case we're going to look at position one so this is what we're going to put inside of here so it's going to go through the table and it's going to look for position one and it's going to return 10 and it's going to print that so if we go into the game hit test and hit play then what we should see in the output is the number 10 because the array was successfully able to look for whatever value was inside of position one and we can do the same thing with I don't know position three and if we go to the game and hit play then it's going to return true because that's what was in the third position I hope this is making sense so far and this is how we're essentially able to access and use arrays inside of our game now what if we want to print out every single value that's inside of this array well we're basically just going to use a for Loop so what I'm going to do is comment out this print statement I'm going to drop two lines and then we're going to make a for Loop so what we're going to do is say four and then we can call this counter variable the index just like this and we're going to set this equal to the first position of the uh array which is always going to be one and then we're going to space this out with a comma and then the max value is going to be the number of elements that are inside of our array which in this case is going to be four now we could say four but there's actually a better way of doing this because what if we change the value of this every single time oh and by the way I forgot to hit stop my apologies on that um if we were to add another value really quickly just like this then this is going to be five elements instead of four and we're going to have to constantly come back and update this value every single time so there's actually a better way of doing this that can automatically up update the N value for however many elements we have inside of here and that is called using a hashtag symbol or a pound symbol so what it's going to look like is basically this and we're going to put down my array so essentially What's Happening Here is we're taking the array and we're using a hashtag or pound symbol before it to basically give us the number of elements that are inside of this array so there's 1 2 3 four elements inside of here so this is going to return four so that's a better way of getting the number of elements inside of an array it's by doing it like this so then we're going to say do and then we're going to hit enter now we're going to make a print statement and we're going to access my array just like this and then inside of the hard brackets we're going to put down the index because it starts at one and then once we go down this iteration it's going to update this index to two and then it's going to go to the second one and then the third one and then the fourth one and it's going to print out every single one so if we go to the game hit play then what we should see is every single value inside of the array being printed just like so because that is how we iterate through all of the elements within an array okay but this is how we print all the elements inside of an array using a for Loop but why can't we just print the array itself so if I were to delete this and then just print out my array there's a lot of technical reasons behind doing it like this but if I were to show you what this looks like if we go into the game and hit play then it's going to show a table with a hash value that's contained within it now again this relates to a lot of backend stuff when it comes to creating objects and storing them inside of memory within the game and I I don't want to go into those technical details right now but basically what you need to know is you can't print the array itself because the array is a table object so it's not going to print out whatever is contained in the table itself since we're just going to have to go through every single element in the array to print them individually instead so that's just something quick I wanted to add and that is basically the introduction to arrays now Roblox has some very useful functions that can allow us to manipulate items in a table while the game is running so if we were to let's say think of an example of when this would happen inside of an actual game is if you were to unlock a pet inside of pet Simulator X or if you were to unlock a new Aura inside of like an RNG game or something like that then we have to add the new value to our inventory table but that's like an oversimplified example I can give when it comes to those sorts of things but the important thing we need to know about is being able to add and remove values from a table while the game is running so I'm going to show you two methods that Roblox has for us within a infrastructure or a library that they have for us so I'm just going to delete this piece of code over here and we're first going to make a new table so I'm going to say local my array equals and then we're going to put in the open and close curly brackets just like this so This represents an empty table so what I'm going to do down here is drop two lines and here's what we're going to do so we're going to use roblox's Library called table in all lowercase just like this and then when we hit dot then there's going to be all these methods we can use and the first one I'm going to show you is called table. insert and this basically allows us to add Val values to this array that we have over here or just any table in general so we're first going to pass in the my array table and then for the second argument we're going to add a value that's going to be put at the end of this array so at the end of this array we can put down let's say the number 10 so if I were to basically explain what this does this is an empty array right now and if we were to see all the elements within this table then it's going to start with nothing but then if we use table. insert to insert a number to this array then the only thing that's going to be here is going to be 10 so let's see what this looks like if we were to print out all the elements within the array so if I were to say for I I'm just going to say I which is short for index and I'm going to set this equal to one comma and then we're going to use the hashtag symbol my array just like this and then we're going to say do and then we're going to print out every single value that's going to be within the array passing in the index counter variable just like this so I'm going to copy this and I'm going to paste it down here so we can see it twice so if we were to go to the game and hit play then what we should see is just the number 10 because when we tried to print the array the first time there's nothing to print and then we added 10 and this is what added more values to it so if I were to hit stop and show you what this looks like if we were to have a populated table beforehand before adding something else so if I were to let's say have 10 20 and 30 as the starting array and if I were to insert let's say 40 then what's going to happen is it's going to insert 40 at the end of the table rather than at the beginning or in the middle or just somewhere random it's going to put it at the end of the the table so if I were to hit play once again then it's first going to start with 10 20 and 30 but then it's going to print out the new table with 10 20 30 and 40 because it added 40 at the end of this array right here and that is essentially how you insert new values inside of an array so I'm going to hit stop and now let me show you what happens if we try to remove a value so I'm going to drop two lines once again and I'm going to say table.
remove just like this uh and then inside of here we're going to pass in my array and now for the second second argument you have to be careful about this one because we passed in a value for the second argument for table. insert but for table. remove we're passing in an index a positional value to basically look for the value in this array that matches the position and it's going to remove it so if I were to let's say put in four uh then it's going to check for the fourth position and then remove whatever values inside of there so if we were to insert 40 beforehand then it's going to be 10 20 30 40 and the whatever is going to be on the fourth position which is 40 that's what it's going to remove rather than 40 which is the value itself it's going to remove whatever's at the fourth position which is going to be 40 so now if I were to copy this once again and then paste it then what we should see if we go into the game is first we're going to see 1020 and 30 then we're going to see 10 20 30 40 and finally we're going to see 1020 and then 30 so that is how you use table.
insert and table. remove to add and remove values from an array when you're working with them inside of the game like while the game is running the last thing I'm going to talk about with arrays is being able to search through values in the array and then look for specific values within them When We're looping through a script so what I'm going to do is basically just delete all of these statements we have except for the very first line and I'm going to add more numbers to this like we could just say 40 50 60 70 80 90 and 100 so we're just going to go from 10 to 100 now if we were to Loop through all these values in the array so if we were to say 4 I = 1 comma pound symbol my array do just like this and then what we're going to do is basically write a conditional here by saying uh if my array in hard brackets then we're going to put down the index like so so if my array with the index counter variable right there is equal to let's say 50 then we're going to make a print statement saying 50 has been found just like this and then if we were to go into the game and hit play then obviously it's going to say 50 has been found inside the table but if we were to remove this and if we hit play then it's not going to print anything because we removed the 50 that we were trying to look for okay now there's another way we can look for values within a table and we don't have to use a for Loop for this so what I'm going to do is basically just comment out this block of code right over here and I'm going to drop two lines so what I'm going to do is write a simple function that Roblox has for us called table. find open close parentheses just like this and I'm actually going to wrap this in an if conditional as well so I'm going to say if table.
find and then what we're going to put inside of here first is the array itself and then for the second argument we're going to put down whatever value it is we're trying to look for which let's say it's 50 so if table. find my array 50 then we're going to print 50 has been found just like this and if we go into the game and hit play then what we should see is 50 has been found now the reason this is useful is because it also Returns the index of where it was able to find this value and if it didn't find it then it's just going to return nil so I could say something like local index equals and then we could just copy this code we had over here put it inside of here and then we're going to say if index is not equal to nil so this is basically the same thing as what we had previously then it's going to say 50 has been found but if you want to make it more simple you could just say if table. find my array 50 it's going to return the index but in this case we don't really need the index so that's why we could just keep it like this then it's going to say 50 has been found so these two basically do the same thing it's just a matter of preference of which one you want to do and that is how you search through values in an array so the next table type is a dictionary and I'm going to first show you this by quickly removing our comments that we had here and I'm also going to comment out the remaining bits of code just so that we can have the array code as reference for our dictionaries okay so what is a dictionary well it's basically another type of table that allows us to search through values inside of a dictionary using a key so when we attach a key to each of these values that's within the dictionary so we can search up these values by using a key that we've attached to each one of these values so I want you to imagine like a real life English dictionary where every single word that you see or every single term that you see has a definition attached to it so if we were to look for a definition of a term then we search up the term and then we get the definition of that term so it kind of works the same way here if we're using a table so if I was to show you how this would look look like we're basically going to create a new dictionary by let's just say local let's call this menu so we're going to create a new menu with different items that we can order inside of this restaurant that we just I don't know created off the top of our head with a price associated with them so what I'm going to do is say local menu and we're going to set this equal to open and close curly brackets just like we did with the arrays but this time what we're going to do is for organization purposes and also for readability we're actually going to drop a line like this so this is still one table but each single item that we add is going to be spaced out for every single line that we add inside of here so here's how it's going to look like there's two ways of writing keys and this is the first way I'm going to show you so we're basically going to write open and close hard brackets and then we're going to put double quotation inside of here and then we can put down whatever menu item name that we want inside of here like let's say we want to add a cheeseburger just like this this is going to be the key but now we're going to set this equal to a value on the right side which let's say we're going to make this cheeseburger 15 bucks just something as ridiculous as this and then we're going to drop a comma and then we're going hit enter so as you remember the way we separate items is by using a comma and so now we can add another item inside of here by basically putting in open and close hard brackets and then we're going to put in the double quotations and then we can add something else inside of here like let's say Cola we're going to make this a $400 Cola just like this and then now we have two items inside of here and we can continue this trend by putting in other things in here like like I don't know a like we can put pancakes inside of here we're going to set this equal to eight bucks and then we can add one more thing inside of here like let's say we wanted to add good old milk just like this we're going to set this equal to four as well so we've added in four entries inside of our dictionary so this whatever's on the left side is going to be our key and then whatever's on the right side is going to be our value now like I said there's a second way of being able to write keys and that's just by using the variable itself so we could just say cheeseburger just like this and this is another valid way of writing it but I'm going to show you more about this later on so this is how we write one and if we want to access a specific value inside of this menu dictionary we're going to write a print statement and then we're going to specify the menu but this time instead of specifying an index we're going to specify the key that we're trying to look for which in this case it's going to be let's say cheeseburger it's going to then return the value of this cheeseburger which is going to be 15 bucks so if we go into the game and hit play then what we should see is the number 15 inside of the output because we were successfully able to look for the value given the key that we gave it so if we hit stop if we were to let's say print multiple values so if we were to say menu and then inside of here we're going to say Cola and then we're going to space this out again by saying menu and then inside of here we're going to say milk and if we go into the game and hit play then we should see all the prices for each of the items that we searched for inside of this dictionary so we have 15 four and then four again so that is how you access values ins side of here now once again there's another way of writing keys so I'm just going to comment out this part of the code and I'm going to just write down the menu Again by saying local menu equals open close curly braces and then we're going to hit enter just like this so we can say cheeseburger as a variable and then we're going to set this equal to 15 and then we can add another one like col we're going to set this equal to four so I'm just going to add two of these really quick so now if we were to access these variables we're going to just simply say menu dot whatever the variable is which going to be cheeseburger and this is going to do the exact same thing so if you go to hit play then it's going to print out the value which is 15 since that is the price of a cheeseburg so these are two ways you can write dictionaries and I hope you found this part to be useful now one more thing I would like to add is we can modify values in the dictionary that we created so I'm just going to hit stop really quick I forgot to do that and I'm just going to comment out this block of code we have over here and I'm going to uncomment what we had up here with our previous menu so what we can do here is we can actually manipulate values given the key that we give it so what we can do is say menu and then inside of the hard brackets we can specify what the key we're trying to modify is so we can say let's say cheeseburger we can change the price of this cheeseburger to something different like let's say we Chang this to $5 instead of 15 then if we were to access this value inside of the game now so if we were to go to the game and hit play then it's going to say five instead of 15 because we were able to change the value just like this and we can basically do the same thing with the other way we created the dictionary so if I were to comment this out one more time and if I were to uncomment this part we can basically just say menu.
cheeseburger equals 5 just like this and it's going to do the exact same thing if we go into the game then it's going to print five just like that and so that is how we use dictionaries inside of our scripts so we know how to use four Loop and also being able to use index values to iterate through a table but there's actually a more conventional way of iterating through tables especially with key value Pairs and that's with the use of pairs Loops which is going to be the final concept we're going to be learning about inside of this tutorial guide and this is going to be a very important one to know about so what I'm going to do really quickly is delete the code that we've had at the end here and I'm also going to uncomment the first dictionary creation we had over here with the menu okay so what do I mean by Pairs and I pairs Loops well basically when we've been accessing items inside of our dictionary with the key and value we've had the key and then that would return we've had the key and then that would return the value of whatever is going to be contained inside of this dictionary but if we want to be able to access all the keys and all the values while we iterate through all the items within a dictionary or just a table in general we need to know how to use Pairs and I pairs Loop because this is also useful if we're trying to iterate through multiple items within one object inside of the Explorer and that's what I'm going to be showing you how to do first with tables and then with models inside of our game so what I'm going to do is basically just remove these two lines of code we have over here and we have our menu currently right here but what we're going to do is say four and we're actually going to create two variables with this pairs Loop so the first one is going to be a variable for the key so we're going to say for menu item just like this and then comma we're going to say whatever the variable of the right side is going to be which is going to be the price and then we're going to say in pairs just like this and then we're going to say open to close parentheses and then we're going to pass in the menu a dictionary like this and then we're going to say do like so so what's going to happen here is we have two variables one of them is going to be for the key and then the other one's going to be for the value which is the price and we're going to iteratively go through this dictionary in pairs with the menu item and also the price so if you were to make a print statement by printing out the menu item along with the price just like this and we go into the game and hit play then what we should see in the output is every single item that's inside the menu with the price that's next to it so this is how we're able to iterate through a table that gives us two variables one of them is going to be the item and the other one's going to be the price and that is basically how you make a pairs Loop now you might have noticed that when we printed out all the items that are contained within the menu it's out of order it started with pancakes then it went to Cheeseburger Cola and then milk so the reason it went out of order is because when we're using pairs it's going to pick the items inside of the dictionary inside of a random order and it's just going to print out everything that's inside of there that's that's why we need to use what's called I pairs which is actually going to iterate through all of the items sequentially from where it is first created to where it is last created and I'm going to be showing you this using the my array that we created at the top of this code right over here so what I'm going to do is comment out all of this code with the dictionary and I'm going to uncomment everything that has to do with our array that's over here I'm actually going to delete this part of the code right here so if we're going to be iterating through all the elements within this array we don't necessarily need to use keys for this but instead we're going to create an index variable so what we're going to do is say four index comma and then we're going to say number in I pairs just like this and then inside of here we're going to put my array just like this and then we're going to say do like that so what we can do from here on out is just basically print out the index and also print out the number as well so what's essentially going to happen here is since we're using I pairs it's going to start at the very beginning and it's going to go in sequential order all the way till the end and index is going to be the position of where each of these items is going to be inside of this array so it's going to be 11 10 220 330 because it's going to print out the position and then it's going to print out whatever the value is for each item inside of the array so if we go into the game and hit play then what we should see is 110 220 330 440 550 just like this and this is how we use ey Pairs and also how we're able to go through every item in sequential order so now I'm going to hit stop and that's basically how we use High pairs inside of our scripts now there's two more tips that I can give you that's very useful and that is basically hiding variables that we don't need so for this index so for this index instance we can actually just use an underscore to basically say that we don't want this variable since we don't need it and this is how we use a pairs or an i pairs Loop if we don't want a specific variable uh we can even do it with value as well but I don't know why you would do that since we don't really need to do that but usually this is helpful for indexes if you don't need the index when you're looping through a pair so this is just something I wanted to add in case you wanted to use this inside of your for Loops since I do use this quite a bit and I'm going to show you how this is useful for what I'm going to show you last so this is going to be the last thing we're going to be doing and that is using a model and iterating through all the parts inside of the model so what I'm going to do is take our model that we created from one of our previous episodes I forgot which one it was I think it was events but basically if you don't have a model like this you could just quickly create one and add three parts inside of this model and what I'm going to do is well first of all going to comment out all the code we have over here and I'm going to make reference to our model that we created inside of the workspace so I'm going to say local model equals game. workspace do model just like this and what I'm going to do is Loop through every single item that's inside of the model so I'm actually going to delete the scripts that we have inside of here and we're going to iterate through all the parts that are in here so we're going to say for and we can say index comma and we can say part in pairs just like this open close parentheses and then we're going to say say model but model is a model data type what we want is a table full of all of the objects that are contained within the model so what we can do is say colon get children just like this open and close parentheses and then we can say do now get children basically returns a table full of every single child that is within the model parent just like this and so what we can do here is basically use all these parts to our advantage and this is how we Loop through models using pairs so we can basically print out let's say the part.