--easy if you know for is a loop can be used in the follwing way:
for i = 1, 5, i++ do --or i-- in min cases
--start index[i] = value[1]
-- max[i+], min[i-] value same as if i == 5 then break end
-- increment i++ or i-- same as i = i-1 or i+1
print(i)
end
-- diffrent between index and value, value is inside a index
--for loop automaticaly breaks after reaching its goal in this example the goal is 5
--output: 1,2,3,4,5
--so what is pairs?
--so we have inventory and the player dropped some items
--how can we achive this?
--reminder making a inventory is harder then this code down below!
local inventory = {"Sword", "Glock", "Rusted Knife"}
function Drop(Item)
if Item == nil then print("We need an Item to drop not nil!") return end
for i, v in pairs(inventory) do
if v == inventory[#inventory] then print("Item not found!") return end
if v == Item then
table.remove(inventory, Item)
print(v, " has been successfully removed!", i)
end
end
end
Drop("Rusted Knife")
--output: "Rusted Knife" has been successfully removed! 3
--enterpretation / explaining the drop function
--Drop function has varable Item Attached to it
--it checks if Item is valid to get removed
--the most important part is the for loop
-- we used pairs to loop though the table and we
--saved the table values as i, v i stands for index
--v stands for value these variables are most used in pairs uses
--you can also use index, value anything you want
--we dont have a custome index so the table takes numbers as index
-- 1 = Sword 2 = Glock...
--and we go though the table using for loop
--and we check if the value of current index is the same as Item
--if it is then we remove it and thats a simple drop function
--Summary pairs are used for going though tables and only tables
--if its too complicated then i'll try to show you
--another may to go though tables without pairs
local food = {"CheeseCake", "Fries", "Expired", "ChickenBurger"}
for i=1, #food, 1 do
local v = food[i]
if v == nil then print("End has been reached no more to Check!") break end
if v == "Expired" then print("Removing "..i.." its Expired") table.remove(food, v) end
end
--thats how pairs work if we decode it the v is just
--returned by pairs tables can get complicated fast thoe
-- this whole section hasnt been tested so we can say it has been
-- blindely created so if you test it there could be errors
-- Credits: Botter3029 you wanna support me just like this post
-- Check Part 2 if you want the complicated pairs use type
--part 2
local Dates = {
["John"] = {
["Gender"] = "Male",
["From"] = "USA",
["RelationShip"] = "Single",
["Age"] = "20",
["RelationShip"] = "Gamming",
["Bank"] = 1000,
},
["Emran"] = {
["Gender"] = "Male",
["From"] = "Saudi",
["RelationShip"] = "Taken",
["Age"] = "25",
["Hobby"] = "Oil Drilling",
["Bank"] = 1000000,
},
["Anton"] = {
["Gender"] = "Male",
["From"] = "Spain",
["RelationShip"] = "Single",
["Age"] = "24",
["Hobby"] = "Football",
["Bank"] = 100000,
},
["Harry"] = {
["Gender"] = "Male",
["From"] = "UK",
["RelationShip"] = "Taken",
["Age"] = "28",
["Hobby"] = "Drinking Tea",
["Bank"] = 10000,
},
["Mohammed"] = {
["Gender"] = "Male",
["From"] = "Iraq",
["RelationShip"] = "Single",
["Age"] = "19",
["Hobby"] = "Programming",
["Bank"] = 100000,
},
}
function ChooseDate(Girl)
local DateOptions = Girl[Options]
local currentdate, currentpoint, finaloption
local point = 0
if DateOptions == nil then print("What kind of boy does the girl want??") return end
for boy, info in pairs(Dates) do
for optioni, optionv in pairs(DateOptions) do
if optionv == "Any" then continue end
if tonumber(info[optioni]) ~= nil then
if info[optioni] >= optionv then print("Rich man very nice!") point++ end
continue
end
if info[optioni] == "Taken" then print("You are taken thoe Wanna cheat ;)") end
if info[optioni] == optionv then print("impressive!") point++ end
end
if currentdate then
if point > currentpoint then
print("Found a better date! sorry "..currentdate)
currentpoint = point
currentdate = boy
point = 0
continue
end
print("ill keep my old date!")
continue
end
currentdate = boy
currentpoint = point
point = 0
end
finaloption = currentdate
print("I choose "..finaloption.." he got "..currentpoint.." ill see u tonight ;)")
end
local Girl = {
["Lisa"] = {
["Gender"] = "Female",
["From"] = "Germany",
["RelationShip"] = "Single",
["Age"] = "18",
["Hobby"] = "Exploring",
["Bank"] = 1000,
},
["Options"] = {
["Gender"] = "Male",
["From"] = "Any",
["RelationShip"] = "Single",
["Age"] = "18",
["Hobby"] = "Any",
["Bank"] = 10000,
},
}
-- this is hard to explain but if you don't get it
-- this is about a girl looking for a date this function
-- doesnt only use for loop once but twice there may have been a easier
-- way to achive this but ill keep it this way since it makes good sense
-- interpretation / explaination
-- there is 2 tables one for the girl and one for the boys
-- in these tables are more tables in this tables are values
-- and information about the persons age, name, sex...
-- we use this info and combining it with the girls date option
-- when a info is combined the point increases
-- the function saves a date if there is no date then it saves
-- the first boy it meets 1 is better then 0
-- if the old date points are worse then the new boys points
-- then the function will overwrite the old date with the new and better one
-- it goes until the end of the boy table then it we will get the lucky boy
-- this is not tested so i don't know wich will be the lucky one
-- this code made for fun because how complicated it could be
-- not everyone will learn from it so keep in mind
-- pairs goes through a table or it is a return function for v:Value
--output: [lucky boy]