What is for i = 1 #table do?

  1. What do you want to achieve? I want to learn the difference between for i,v in pairs and for i = 1 in pairs. I also want to learn when should I use for i =1 in pairs.
  2. What solutions have you tried so far? Youtube
for i = 1 ,#text do
	script.Parent.Text = string.sub(text,1,i)
	wait()
end
1 Like

You should check this out!


A # gets the length of something, so for example:

There are 5 models inside 1 model.

print(#TestModel:GetChildren()) -- 5

There are 25 parts together in the entire model

print(#TestModel:GetDescendants()) -- 25


Now as you have learnt about # let’s now step it up to the for loops!


What are for loops?

Well, for loops are really really helpful! If you want to make all the parts in a model Transparent to 1 at once, you might think. Variables for each part should do it! But that’s a long process. If you want to make it shorter use for loops.

You can go ahead loop through the entire model and check if the value is a BasePart or not. If it is then we will change its transparency to 1. How do you do this?

local model = script.Parent

for i, v in pairs(model:GetDescendants()) do -- i = index, v = value
    if v:IsA("BasePart") then -- Checking if the v is a BasePart or not. If it is then
        v.Transparency = 1
    end
end

Now, your main question you asked. What is for i = 1, #table do?

It’s a little little bit difficult than the other one, but hey, not too hard.

So, how does it work?

I’ll show an example with how you showed. So, you’re making a typetext. Now, this is how you do it.

local text = "This text is cool!"

for i - 1, #text do -- 1 is the starting number. So, 1 will be T from "This" in the text. And #text is the ending number. Instead of counting how many alphabets there are, just put a #
    -- I'm not gonna explain the other stuff for now. You can check out other posts but hopefully this helped!
end
4 Likes

Thanks! But what does for i = 1 #table do?

1 Like

I explained everything in the first reply!

1 Like

Oh sorry! Just seen you updated it.

1 Like

In pairs() loop

When you use:

for i, v in pairs(table) do
   print(v)
end

I = index
V = value
Index is the index value of the table where as the value is the value of that index in the table (table[i]). Here’s an image that describes what I’m talking about:

download

For loop

When you use:

for i = 1, #table, 1 do
   print(table[i])
end

This is equivalent to the method shown above. The # is used to say the length of the array. This means the loop will start at 1 and iterate how ever many times the table has elements. i still = the index. Since there is no v variable to store the value(s), we get the value by saying table[i] which will find the value that corresponds to the index in the table.

Basically it’s the same loop formatted slightly different in code. Hope this helps!

5 Likes

Sorry, I couldn’t explain the index part properly I myself have not used it much. If I find some information regarding index I’ll edit the post!

Edit: Oh and @zblox164 already explained it lol

1 Like

Thank you! So much I wish i can make to post marked as solution

2 Likes