To clarify pairs() and ipairs(): pairs() is used to iterate through dictionaries ipairs() is used to iterate through arrays **only**. This is because elements in an array are numerically ordered, while a dictionary is unordered. I don't think I made that clear so my apologies.
@@صاحب-العقل-والمنطق for readability and possible performance problems (even if it is too little). if you dont use pairs or ipairs, it does not indicate what kind of table is the one we are iterating just by looking at it, and we will need to go to see what we are iterating, and it is annoying, and roblox does not recommend it
Pairs() work completely fine for me in arrays and it’s in order the only difference between pairs() and ipairs() in arrays that I know of is that ipairs() if it reaches nil inside of a table than it dosen’t do the rest of the table unlike pairs()
-- Buying from a menu local menu = { ["Burger"] = 15, ["Fries"] = 5, ["Cola"] = 4, ["Milk"] = 3, } menu["Burger"] = 12 print(menu["Burger"], " dollars for a burger,", menu["Fries"], " dollars for fries and", menu["Cola"], "dollars for a cola") print("That would be", menu["Burger"] + menu["Fries"] + menu["Cola"], "dollars in total!") -- Output 1: 12 dollars for a burger, 5 dollars for fries and 4 dollars for a cola -- Output 2: That would be 21 dollars in total!
@cobyjacob2054 1 month ago -- Buying from a menu local menu = { ["Burger"] = 15, ["Fries"] = 5, ["Cola"] = 4, ["Milk"] = 3, } menu["Burger"] = 12 print(menu["Burger"], " dollars for a burger,", menu["Fries"], " dollars for fries and", menu["Cola"], "dollars for a cola") print("That would be", menu["Burger"] + menu["Fries"] + menu["Cola"], "dollars in total!") -- Output 1: 12 dollars for a burger, 5 dollars for fries and 4 dollars for a cola -- Output 2: That would be 21 dollars in total!
Mannnnn, I am finally done and yeah! I have learned quite a lot throughout this playlist/tutorial guide... Excited to do the next video (Making an actual game >:))
apparently, this just works for dictionaries, if you just do normal arrays, this wont appear. edit: even for dictionaries its not working to me, so idk
local lvl6 = game.Workspace[" Levels"].Level6 local part = {lvl6.Part, lvl6.Part1, lvl6.Part2, lvl6.Part3, lvl6.Part4, lvl6.Part5, lvl6.Part6, lvl6.Part7, lvl6.Part8} local num = 1 while true do part[num].CanCollide = true part[num].Transparency = 0 task.wait(1.5) part[num].CanCollide = false part[num].Transparency = 0.8 num = num + 1 if num == 10 then num = 1 end end -- that going to make a obstacle move forward and backward
Thank you so much! I want to make a game on Roblox which looks like it could be really popular. I am on my own and I don't like to hire people! I'm good at making models and maps and the scripting was the only thing stopping me! The game i am making is my first one and it will be really good!
Could you provide a quick summary of your game? I agree with you; I can model, build, and design, but I don't have experience with scripting. I think it's more beneficial and rewarding to learn everything on my own rather than paying someone else to do it!
@@Vertect sorry man. I kinda wanna keep it a secret but it is based on a murder mystery game. Mine has lots of differences I don't want to share at the moment. I will reply again once the game is done and I will provide the game. I don't want anyone copying at the moment.
i love this chanel, i have always had a deep intress in coding but found it to daunting to begin to learn but this chanel broke it down to bite sized pieces for me
Not related to the video but I made a fog using the info from this tutorial that slowly deals damage like the storm from Fortnite. It doesn’t move yet but this tutorial has taught me tons. Thanks for the help dude!
Both basically do the same thing. The difference is that 'workspace' is a built-in keyword and 'game.Workspace' is how you access the Workspace folder inside the game data model.
Summary Tables are a type of data collection in LUAU (a data collection is basically a variable which can contain many values inside it) There are 2 types of tables in LUAU: Arrays and Dictionaries - Array: They are tables whose elements are indexed with numbers based on the position of the element we want to get, for example: array[4]. This gets the element in position 4 of the array - Dictionaries: These are tables whose indexes are a data type other than a number (i.e. you can not index by doing dict[4]). So to index, you must do it with the name of the "key" (the key is the name that refers to a certain value, this name is used only to access the value contained) as follows: dict["name"]. This will get the dictionary value with the key "name" What is indexing? Indexing is to ACCESS a specific value in an array or dictionary based on the key that references it, by default, the arrays have as index a number (from 1 to the number of elements). While the dictionaries, will depend on what value you put, whether it is a text, a reference in memory, a part, or whatever "#" as operator The # symbol is used to get the number of elements in an ARRAY (this does not work with dictionaries, if you try it, you will get 0 even if you have values).
Bro's speedrunning the scripting series Just kidding. I've been able to study scripting consistently because of your consistent uploads. Thank you, man💯
your kinda wrong with the pairs and ipairs part, because ipairs iterates through arrays, while pairs iterates through both arrays and dictionaries. Meaning you can't use ipairs with dictionaries, only arrays.
Good catch! Although the documentation says pairs is only used with dictionaries, while ipairs is used with arrays. They recommend using a combination of the two for full capabilities (depending on the problem).
local Array = {5, 10, 55-54} for A = 1, #Array do print(Array[A]) end -- This is A Type Of Table Called Array. -- Array Is A Data Structure That Can Hold Multiple Values. table.insert(Array, 50) print(Array[4]) if Array[4] == nil then print("There's not a 4th position") else print("There's a 4th position") end -- table.insert is a function that is used to add more values inside of an array. table.remove(Array, 4) print(Array[4]) if Array[4] ~= nil then print("4th position still exist") else print("4th position has been taken") end -- table.remove is a function that is used to remove values from an array. if table.find(Array, 10) then print("10 has been found") end -- This is the first way to find a value in an array -- table.find is a function that is used to find values from an array. for i = 1, #Array do if Array[i] == 1 then print("1 has been found") end end -- This is the 2nd way to find a value in an array (using for loop)
local partsFolder = Instance.new("Folder",workspace) for index = 1,8 do task.wait(1) local part = Instance.new("Part") part.BrickColor = BrickColor.random() part.Position = Vector3.new(0,20,0) part.Parent = partsFolder print("part created") end local partsTable = partsFolder:GetChildren() --returns all the children inside our specified instance and converts them into a array table for _,parts in ipairs(partsTable) do task.wait(1) parts:Destroy() print("Part",_,"Destroyed") end moving up! these tutorials have been so helpful and im very excited to move on to the advanced guide
@@TropialGamingYT just keep going at it always try to make a couple custom examples for yourself so it helps you really understand and keep what you learned inside your head
6:48 If your using Roblox studio, it will actually print out all the elements in the array. He was probably using a older version of roblox studio when he uploaded this video.
local activatie = { ["activatie1"] = 18,--roler coster ["activatie2"] = 33,--bungie jumping ["activatie3"] = 10,--trampoline ["activatie4"] = 13,--eating contest } for menuItems, price in pairs(activatie) do print(menuItems, price) end your are a amazing teachir for scripting its been my dream to know how to script and become a game maker and when i reach that goal i will remeber that you where the first persone to ever motivate me to keep moving thank you with all my heart
ive been scripting for 6 months and would appreciate if you make a video about metatables and object oriented programming since it is a useful tool in roblox studio
My little script local smallSupermarket = { ["Apple"] = 5, ["Banana"] = 3, ["Mango"] = 8, ["Bread"] = 2 } for index, i in pairs (smallSupermarket) do print(index, i) end
--Locatates model, them gets the items within the model, then uses for loop to order items and rename (SO hyped for the next vid!) local model = game.Workspace.Model local items = model:GetChildren() for index, item in ipairs(items) do items[1].Name = 'Part 1' items[2].Name = 'Part 2' items[3].Name = 'Part 3' print(item) end -- kept it simple today
omg dude i cant explain how much it ur code helped me to fix my coins system i couldnt figure it out even after watching the video that how do i get the coins directly without calling out every single coin thanks dude really appreciate that
This logic teleports a part randomly within the parameters local part = game.Workspace.randomPart while true do local randomX = math.random(0,100) local randomY = math.random(0,1.5) local randomZ = math.random(0,100) part.Position = Vector3.new(randomX, randomY, randomZ) task.wait(1) end
local theWorkspace = game.Workspace local Plate = game.Workspace.Plate local isThePLatetouched = false Plate.Touched:Connect(function(partTouching) if isThePLatetouched == false then isThePLatetouched = true local humanoid = partTouching.Parent:FindFirstChild("Humanoid") if humanoid then print("Your workspace consists of...") for index, part in pairs(theWorkspace:GetChildren()) do print(part.Name) task.wait(1) end end isThePLatetouched = false end end) -- what this does is basically whenever we touch the Plate inside of the workspace, it states every item in the Workspace. It also takes a cooldown for a second after stating one item after the other item.
Note if we want to get all object that are inside of Model, not just its Children, and also we want to check if we getting a Parts not other things it will looks like this; -- Get all the Parts inside the Model for _, part in pairs(model:GetDescendants()) do -- Check if the part is a Part if part:IsA("BasePart") then -- Print the name of the Part print(part.Name) end end
--[[local menu = { --table Chessburger = 15, ---a value inside of the table BloxyCola = 5, ---a value inside of the table Pizza = 25, ---a value inside of the table Tacos = 10 ---a value inside of the table } for MenuItem, Price in pairs(menu) do ---does a for loop and does a pair loop randomly print(MenuItem, Price) ---prints the menu item and price end--]] ---menu["Chessburger"] = 5 ---print(menu["Chessburger"]) --[[local menu = { ---table ["CheeseBurger"] = 15, ---a value inside of the table ["BloxyCola"] = 5, ---a value inside of the table ["Pizza"] = 25, ---a value inside of the table ["Tacos"] = 10 ---a value inside of the table } menu.CheeseBurger = 5 print(menu.CheeseBurger)--]] --print(menu["CheeseBurger"], menu["BloxyCola"], menu["Pizza"], menu["Tacos"])--]] --[[local MyArray = {10,20,30,40,50,60,70,80,90,100} ---Array(type of table) for _ (can be i or _), number in ipairs(MyArray) do ---does a for loop that is ipairs (sequence) print(number) end--]] ---for i =1, #MyArray do --for loop in --print(MyArray[i]) --prints my array --end --table.insert(MyArray, 40)---inserts number ---for i =1, #MyArray do --for loop in # --print(MyArray[i])--prints my array --end --table.remove(MyArray, 4)---removes the array local model = game.Workspace.MyModel for index, part in pairs(model:GetChildren()) do print(part.Name) end
Hello, I've watching your tutorial and I am having fun with it, I would just like to ask if you could create a tutorial on creating a basic game manager that is in the module, I know you have a tutorial on module script already, but what I would like to see is on an actual game manager that can perform basic game like an loop game, I know its kinda lot to ask for, but I would like to have a server game manager incorporated with a module script that can be use for loop game
wait a minute, if you've seen this comment please answer my question 🙏🙏🙏🙏: Q1.)Does index represent the variable in the for loop or the index of the numbers in the array which is "myArray" Q2.) Could someone pls clarify the for loop for me pls🙏🙏
local mcbonaldsmenu = { ["ShinyBurger"] = 2, ["FrenchFriesOfDoom"] = 5, ["Sparkingbottle"] = 3, } for menuItem, Price in pairs(mcbonaldsmenu) do print(menuItem,Price) end
unfortunately I tried to grab the value from the leaderstats but I just didnt know how to get it so I got stuck pretty much that. I wanted to use the value of the stats to determine if a print statement showed saying what things you could afford but I just didnt got it to work.
-- This is what I did (I basically made it so when you touch that touchPart we made in a previous episode, you "get" a pet: local rollablePets = { commonPet = "COMMON DOG", commonPet2 = "COMMON CAT", commonPet3 = "COMMON LIZARD", commonPet4 = "COMMON GUINEA PIG", commonPet5 = "COMMON BUNNY", uncommonPet = "UNCOMMON ELEPHANT", uncommonPet2 = "UNCOMMON LION", uncommonPet3 = "UNCOMMON TIGER", uncommonPet4 = "UNCOMMON RHINO", uncommonPet5 = "UNCOMMON GAZELLE", rarePet = "RARE SHEEP", rarePet2 = "RARE COW", rarePet3 = "RARE MONKEY", rarePet4 = "RARE HORSE", rarePet5 = "RARE PENGUIN", mythicalPet = "MYTHICAL RAINBOW AZUNE", mythicalPet2 = "MYTHICAL DRAGON", mythicalPet3 = "MYTHICAL RUDOLPH", mythicalPet4 = "MYTHICAL UNICORN", mythicalPet5 = "MYTHICAL ????", } local tp = game.Workspace.touchPart local partIsTouched = false local debounce = 2 game:GetService("Players").PlayerAdded:Connect(function(plr) plr.CharacterAdded:Connect(function(character) tp.Touched:Connect(function(otherPart) if partIsTouched == false then partIsTouched = true if otherPart.Parent == plr.Character then local petRandomizer = math.random(1,20) if petRandomizer == 1 then print("You got a(n) "..rollablePets.commonPet.." 1/20") end if petRandomizer == 2 then print("You got a(n) "..rollablePets.commonPet2.." 2/20") end if petRandomizer == 3 then print("You got a(n) "..rollablePets.commonPet3.." 3/20") end if petRandomizer == 4 then print("You got a(n) "..rollablePets.commonPet4.." 4/20") end if petRandomizer == 5 then print("You got a(n) "..rollablePets.commonPet5.." 5/20") end if petRandomizer == 6 then print("You got a(n) "..rollablePets.uncommonPet.." 6/20") end if petRandomizer == 7 then print("You got a(n) "..rollablePets.uncommonPet2.." 7/20") end if petRandomizer == 8 then print("You got a(n) "..rollablePets.uncommonPet3.." 8/20") end if petRandomizer == 9 then print("You got a(n) "..rollablePets.uncommonPet4.." 9/20") end if petRandomizer == 10 then print("You got a(n) "..rollablePets.uncommonPet5.." 10/20") end if petRandomizer == 11 then print("You got a(n) "..rollablePets.rarePet.." 11/20") end if petRandomizer == 12 then print("You got a(n) "..rollablePets.rarePet2.." 12/20") end if petRandomizer == 13 then print("You got a(n) "..rollablePets.rarePet3.." 13/20") end if petRandomizer == 14 then print("You got a(n) "..rollablePets.rarePet4.." 14/20") end if petRandomizer == 15 then print("You got a(n) "..rollablePets.rarePet5.." 15/20") end if petRandomizer == 16 then print("You got a(n) "..rollablePets.mythicalPet.." 16/20") end if petRandomizer == 17 then print("You got a(n) "..rollablePets.mythicalPet2.." 17/20") end if petRandomizer == 18 then print("You got a(n) "..rollablePets.mythicalPet3.." 18/20") end if petRandomizer == 19 then print("You got a(n) "..rollablePets.mythicalPet4.." 19/20") end if petRandomizer == 20 then print("You got a(n) "..rollablePets.mythicalPet5.." 20/20") end end task.wait(debounce) partIsTouched = false end end) end) end)
6:19 for this why dont you have to add a final number to show in what incrimints it goes through the script? ex: mycounter = (for 2, 6, ?) you didnt clarify what would be the ? in this scenerio
No, it's just to tell the compiler this: "hey, i wont use this variable, ghost it" (ghost is like saying, ignore this variable, if you use it, roblox will show a warning
@@BrawlDevRBLX it is not really to have less variables, the memory continues occupying it and the value continues being there, it is only to indicate to the linter and luau that we are not going to use that variable. And also to say to the one that reads it that it is not necessary, it's a weird luau feature 🤓
@@osakadev it wont give an error even if you dont use the underscore, the index value is just there if you need to use it at any given time. I dont get why you would replace it with a underscore when it doesnt change your code at all
my entry for learning opportunity this code makes it to where when i hit a button it gives to opposite colors local Discofloor = game.Workspace.Gaming local button = Discofloor:FindFirstChild("Button") local part = Discofloor:FindFirstChild("Bing bong") local part2 = Discofloor:FindFirstChild("Bing boing") button.Touched:Connect(function(BasePart) local c = math.random() part.Color = Color3.fromHSV(c,1,1) local d = 1-c part2.Color = Color3.fromHSV(d,1,1) task.wait(0.1) end)
Also I tried to script after these series but I forgot almost everything after this series, well my memory sucks everytime but can you give me tip how to remember it ?
--[[local myArray = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100} for _, number in ipairs(myArray) do print(number) end]] --[[local menu = { ["Cheeseburger"] = 4, ["Cola"] = 4, ["Sword"] = 50, ["MedPack"] = 50, } for menuItem, price in pairs(menu) do print(menuItem, price) end]] local model = game.Workspace.Model for index, part in pairs(model:GetChildren()) do print(part.Name) end
Taking Notes :D (ironically here’s a note for me or anyone else reading, most these timestamps aren’t accurate I just subtract like 1-3 seconds from where I stopped to take down notes) 1:03 So tables are kinda like variables but they can *store multiple kinds of data* (examples so far have been numbers, booleans, and strings) 2:13 Tables are made using {} and are done in an order? 3:04 and 3:12 They’re done from left to right, which the first/leftmost value having a positional value of 1, which each next one being a higher number (the second value’s got a position of 2, the third has a position of 3, etc.) 2:42 To add another piece of data/value you just to need to use a comma before adding another one 3:12 to access data inside Arrays you need to use their positional value in the table. And then use [] with their positional value to access it like this 4:04 4:51 to print out all everything in the array you gotta make a for loop (lol I basically paraphrased what you said) 4:57 the loop’s starting value will use the first positional value in the array, which is always 1 (although maybe I could skip the first value by making it 2 but I have no idea if that would work or if that has a use) 8:14 Perfect explanations as always (this is literally the 2nd video I’ve watched from you XD) 9:06 so that’s what “i” means, “index”. I was always confused on what it meant when I saw it in scripts and dev forums Now what does index mean XD 10:22 Once again, perfect 👌 also yay now I know what an Index is… a table’s positional value 💀 Well I will be calling it index from now on it’s so short and simple compared to typing out positional value. Also I’m gonna type the rest of this in the comments of my own comment since this main one is bugging out like hell because I’m on mobile
15:01 So basically Dictionaries are Arrays but the items inside are also variables. Or I guess a better way to describe them would be before with Arrays you had a shelf with a plaque and boxes tagged with numbers on the side (that also automatically change based on where they’re placed… don’t ask how) but dictionaries are the same thing but the boxes have a plaque/name too! 16:57 dang guess my first analog
this is my code thank you -- Coffee Shop menu and price local coffeeshop = { ["Coffee"] = 2, ["latte"] = 3, ["Fruit"] = 2, ["Sandwhich"] = 3, ["water"] = 0, ["muffins"] = 2, ["pastries"] = 2 } for menuItem, price in pairs(coffeeshop) do print(menuItem, price) end