Whats the Difference Between Ipairs and pairs?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    The Difference Between Ipairs and pairs.
  2. What is the issue? Include screenshots / videos if possible!
    I can’t seem get them.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to see the difference between them from all sources expect for this and yet I dont get it.

Please show me examples of scripts and show the difference.

3 Likes

ipairs iterates through arrays, while pairs iterates through both arrays and dictionaries.

local array = {1, 2, 3, 4, 5}
local dictionary = {index = "value", key = 2}

-- This won't run because `dictionary` doesn't contain any numerical indices
for i, v in ipairs(dictionary) do
   print(i, v)
end

-- This will run because `array` is an array (table with numerical indices)
for i, v in ipairs(array) do
   print(i, v)
end

-- This will run because `pairs` covers both arrays and dictionaries
for i, v in pairs(dictionary) do
   print(i, v)
end

-- This will also run for the same reason as listed above
for i, v in pairs(array) do
   print(i, v)
end

However, you no longer need to use ipairs or pairs since generalized iteration was introduced.

-- These both behave and do the same thing

for i, v in dictionary do
   print(i, v)
end

for i, v in array do
   print(i, v)
end

Yes

14 Likes

oh… So Ipairs and pairs are useless at this point?

2 Likes

Question,

local Pointx = workspace.Pointx
local GatherChilderen = Pointx:GetChildren()

for i, v in GatherChilderen do
	print(i.."="..v)
end

Why does this give me a erorr, it says Workspace.Bulop.Script:5: attempt to concatenate string with Instance

Instance:GetChildren() returns {Instance}, so you’d just want to do:

local Pointx = workspace.Pointx
local GatherChilderen = Pointx:GetChildren()

for i, v in GatherChilderen do
	print(i.."="..v.Name) -- use `.Name` here
end

oh…
I see thats my mistake.
Thanks

One more question,

local Pointx = workspace.Pointx
local GatherChilderen = Pointx:GetChildren()

for i, v in GatherChilderen do
	while wait(3) do
		script.Parent.Position = v.Position
	end
end

So, Im trying to make the part move to all the checkpoints. But the Part isn’t moving but instead going to only one spot which is Checkpoint1. how do i fix it?

1 Like

You don’t need to know the difference because they are not needed for iteration anymore.

I think you’d want to do something like this:

local Pointx = workspace.Pointx
local GatherChilderen = Pointx:GetChildren()

for i, v in GatherChilderen do
	task.wait(3) -- wait 3 seconds
    script.Parent.Position = v.Position -- assign the part to this BasePart's position
end

If your intention is for this to continue forever, then you should wrap the for loop inside of a while loop:

local Pointx = workspace.Pointx
local GatherChilderen = Pointx:GetChildren()

while true do
   for i, v in GatherChilderen do
	  task.wait(3) -- wait 3 seconds
      script.Parent.Position = v.Position -- assign the part to this BasePart's position
   end
   task.wait() -- halt for a single frame
end
1 Like

thanks, It works. your very helpful tbh

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.