Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR LUA

roblox pairs

--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

 
PREVIOUS NEXT
Tagged: #roblox #pairs
ADD COMMENT
Topic
Name
4+4 =