Jump_off_a_cliff

Lua for i,v tutorial

Jun 1st, 2022 (edited)
169
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 1 0
  1. -- How to use for i,v loops and what are they?
  2.  
  3. -- if i say something wrong pls dm python#9373
  4.  
  5. -- for i,v in pairs() is used to loop through a table
  6. -- If you don't know what tables are have a look at this: https://www.lua.org/pil/2.5.html
  7.  
  8. -- Let me show you something:
  9. for i,v in pairs(game.Workspace:GetDescendants()) do
  10.    print(i.. " - " ..v)
  11. end
  12.  
  13. -- This program loops through the workspace and gets everything
  14. -- The i stands for index, some people call it key so they do k,v instead of i,v
  15. -- and the v stands for Value
  16.  
  17. -- I print the index concatenated with a dash and the value
  18.  
  19.  
  20. -- So lets say the Baseplate
  21. -- It could be the 3rd object inside of the workspace since the Terrain and Camera (I  think) are first
  22.  
  23. -- So while looping, the index will be 3 when at the Baseplate and the value will be the Baseplate
  24.  
  25. -- You can do many things with this (exploiters use this alot to their advantage by doing something like below)
  26.  
  27. for i,v in pairs(game.Workspace.Coins:GetChildren()) do -- Looping through a possible folder in the game called coins
  28.    if v:IsA("BasePart") and v.Name == "Coin" then -- If the object is a part and its name is 'Coin'
  29.         v.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame -- They will make it teleport to them
  30.    end -- The end of the 'if statement'
  31. end -- The end of the loop thing
  32.    
  33. -- But this is also super helpful for game developers
  34.    
  35. for i,v in pairs(game.Workspace.Lava:GetChildren()) do -- Looping through a folder in the workspace called 'Lava'
  36.         if v:IsA("BasePart") and v.Name == "Lava" then -- If the object is a part and its name is 'Lava'
  37.             v.Touched:Connect(function(object) -- Then when it's touched
  38.                 if object.Parent:FindFirstChild("Humanoid") then -- If the object that touched it has a humanoid
  39.                     object.Parent.Humanoid.Health = 0 -- We will set the health to 0, killing them
  40.                 end -- end of 'if statement'
  41.             end) -- end of function (.Touched event)
  42.        
  43.         end -- end of 'if' statement
  44. end -- end of for i,v loop thing
  45.  
  46. --[[
  47. If this was hard to follow, I understand; my explaining was kinda bad (really bad)
  48. Dm me at python#9373 if u want more info
  49. --]]
  50.  
  51. -- Also you can loop through almost anything, not just workspace objects or workspace
  52.  
  53. -- quick example:
  54.  
  55. for i,v in pairs(game.Lighting:GetDescendants()) do -- Looping through Lighting
  56.    if v.Name == "Beams" and game.Players.LocalPlayer.Graphics.Value == "Low" then
  57.         v:Destroy()
  58.    end
  59. end
Add Comment
Please, Sign In to add comment