|
| 01 Jul 2017 10:11 AM |
I wanna learn as much as i can abt scripting so can anyone tell me what this means? for i, v in pairs (what do u put here?) do -- stuff here end Please tell me if you know.. |
|
|
| Report Abuse |
|
|
nullfeels
|
  |
| Joined: 31 Mar 2017 |
| Total Posts: 1215 |
|
|
| 01 Jul 2017 10:13 AM |
| Is this a serious question? I'm starting to question every thread on these forums now... |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:20 AM |
It's a for loop that loops through an array. You provide the array as a parameter where you have the "what do you put here?". |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:22 AM |
| Why wouldnt it be? Well maybe this is one of the basic stuff and i do not know it... just please help me |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:22 AM |
The pairs function will iterate over all key, value pairs in a table
say if you have a table :
tab = {1,2,"hi", "lol", 64}
for i,v in pairs(tab) do print(i,v) end
This will output:
1,1 2,2 3,"hi" 4,"lol" 5,64
You see, the i value is what integer the loop is on, and the v is what value of the table it is dealing with.
Sorry if that's a lil confusing If you don't know how basic for loops work then I would suggest reading up on that |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:22 AM |
| I was replying to nullfeels |
|
|
| Report Abuse |
|
|
nullfeels
|
  |
| Joined: 31 Mar 2017 |
| Total Posts: 1215 |
|
|
| 01 Jul 2017 10:32 AM |
EDIT: This mostly is what @systematicaddict said, but since I already wrote it... Sorry if it makes no sense, I should be asleep right now. :P
Ehh, there is a lot of fake posts lately. xD
As stated above pairs() is a function that takes a table as an argument and returns the next key/value pair in the collection.
Most tables are simple arrays so the key doesn't really matter much(just a auto assigned integer), but when a table is used as a dictionary it may or may not matter to you just depends.
Lets say I had a table called fruits...
local fruits = {"Apple", "Orange", "Pear"}
for k,v in pairs(fruits) do print(k) -- print the key print(v) -- print the value end
would print: 1 Apple 2 Orange 3 Pear
Now lets say I had a table like the following:
local monies = {player1 = 10, player2 = 140, player3 = 35}
the ###e####p####ve would produce the following results:
player1 10 player2 140 player3 35
### ### is the index of the item in the collection(table).
For example I could access player2's monies like so:
local player2Money = monies["player2"]
The variable player2Money would now hold the value of 140 |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:33 AM |
Oh thx i think i kinda get it now but what if i use objects?
table = {script.Parent, game.Workspace.Part, script.Parent.Part}
for i,v in pairs(table) do print(i,v) end
is the output going to be: 1, script.Parent 2, game.Workspace.Part 3, script.Parent.Part ?? |
|
|
| Report Abuse |
|
|
nullfeels
|
  |
| Joined: 31 Mar 2017 |
| Total Posts: 1215 |
|
|
| 01 Jul 2017 10:34 AM |
| You know whats funny? Roblox filtering ..... |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:35 AM |
| Please dont use fancy words.. what is key?? |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:37 AM |
| Im glad you guys r trying to help but ive got a ton of questions to ask now if you dont mind answering them |
|
|
| Report Abuse |
|
|
nullfeels
|
  |
| Joined: 31 Mar 2017 |
| Total Posts: 1215 |
|
|
| 01 Jul 2017 10:39 AM |
Yes, basically.
Key isn't a fancy word. Think of it like a key card xD A key card is used to gain access to a locked door.
A key in a dictionary(a type of collection) is used to access an element of the collection.
In this example "local names = {a = "Bob", b = "Fred", c = "Jim"}"
The values a,b, and c are the keys a points to Bob, b points to Fred, etc. |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:41 AM |
What if.. pairs(game.Workspace:GetChildren()) Is that a table? And what would the table look like? Lets say workspace's children are a stringvalue, an intvalue and a numbervalue
|
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:42 AM |
I believe that it would print the Name value of the object by default, unless you wanted to specify further like
print(v.CanCollide)
>> true |
|
|
| Report Abuse |
|
|
| |
|
|
| 01 Jul 2017 10:45 AM |
for i,v in pairs
v is the Value in the table |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:47 AM |
Ya but if for i,v in pairs(game.Workspace:GetChildren()) do -- v = ??? end |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:50 AM |
| are you guys asleep now? Oh well i gues i shouldnt be asking so much xD |
|
|
| Report Abuse |
|
|
nullfeels
|
  |
| Joined: 31 Mar 2017 |
| Total Posts: 1215 |
|
|
| 01 Jul 2017 10:54 AM |
No, actually I'm eating. :P
workspace:GetChildren() would return a collection containing every child of the workspace.
workspace - Script - Part - Part - Wedge - Decal
for k,v in pairs(workspace:GetChildren()) do print(v) end
output: Script Part Part Wedge Decal |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 10:56 AM |
v.CanCollide = false So all workspace children will have cancollide false? |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 11:00 AM |
Say your Workspace hierarchy consisted of:
Workspace -Part -Baseplate -Model
and you wanted to run through all of the objects and print the name and CFrame of each, then you would do:
for i,v in pairs(workspace:GetChildren()) do print(v) print(v.CFrame) end
then it would output
Part a CFrame value(kinda long so I don't want to put examples) Baseplate a CFrame value Model
Then it would hit an error, because Models don't have a CFrame value.
but you still want to run through the rest of the objects, right?
so you can set up an if to make sure the object is a part before running this loop.
for i,v in pairs(workspace:GetChildren()) do if v:IsA("BasePart") --the IsA function simply checks the ClassName of an object print(v) print(v.CFrame) end end
That would output:
Part part's CFrame value Baseplate baseplate's CFrame value
and it would stop there because when it runs through the Model, it doesn't meet the if requirements, and just continues looping
Sorry if that was kinda random I just thought it would help you understand better |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 11:04 AM |
Omaigad i just asked a simple question and you made it so complicated xD I am amazed by scripters |
|
|
| Report Abuse |
|
|
nullfeels
|
  |
| Joined: 31 Mar 2017 |
| Total Posts: 1215 |
|
|
| 01 Jul 2017 11:07 AM |
"v.CanCollide = false So all workspace children will have cancollide false?"
No.
This might help you to understand some of these basics. wiki.roblox.com/index.php?title=Scripting_Book |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 11:50 AM |
| How would that not make BaseParts in the Workspace non-collidable? |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2017 09:17 PM |
| Please just explain to me what will happen.. I don't rly understand the wiki |
|
|
| Report Abuse |
|
|