Problems with i,v pairs

Greetings! I’m currently working on a medieval coop game and I need some help with an specific function, the function itself runs flawlessly, the only problem is that it only affects the first children of the folder I provided, even though it’s an i,v in pairs function, it is properly enclosed and no errors pop up at the console, would be really happy if anyone can spot any problems with it.

local function StartCooking()
	for i,v in pairs(posis:GetChildren()) do
	v['1'].Transparency = 1
	v['2'].Transparency = 0
	v['1'].CanCollide = false
	v['2'].CanCollide = true
	v['2'].Transparency = 1
	v['3'].Transparency = 0
	v['2'].CanCollide = false
	v['3'].CanCollide = true

	v['3'].CanCollide = false
	v['3'].Transparency = 1

	local itemspawn = game.ServerStorage.Items
	local targetitem = itemspawn['Bread']:Clone()

	targetitem.Parent = workspace
	targetitem.Anchored = false
	targetitem.CanCollide = true
	targetitem.CFrame = v.CFrame
	for i,v in pairs(cookers:GetChildren()) do
	levelcore.AddEXP(v.Name, 5)
	bread_amount.Value = 0 

Hello, if you want the for loop to get every single children and not just the first children you do

for i,v in pairs(posis:GetDescendants()) do

insted of

for i,v in pairs(posis:GetChildren()) do

This way it will cheack every single child


Oh, I understood the problem now that I ran the script longer, it is affecting all children, just one at a time like it’s supposed to, thanks for trying to assist me though. Apparently, it’ll cook each bread individually instead of the whole group, I kind of liked it better this way but thanks for your help. Pretty sure :GetChildren() and :GetDescendants() are the same thing though.

1 Like

Glad you solved the problem, but to clarify GetChildren just loops in the children of the part while GetDescendants gets the children of the children so you can get even deeper and get everything

Example: if i have a Part that has a model inside with 5 Parts, if i do GetChildren in the first part it will only get the model, if I do GetDescendants it gets the model and the 5 Parts inside of the model.
:+1:Good luck with your game

1 Like


yeah it might work but

the ipairs generator is specially built for the purpose of iterating over arrays with all numerical indices (can’t index non-list items) , use that here for quicker results (by a few thousand microseconds)

  for _, child in ipairs(posis:GetChildren()) do

use pairs when you need to either iterate through dictionaries or use the index later on

1 Like

I think you’re using the wrong unit here, because a thousand milliseconds is one second. Neither function should take anywhere near that long.


It might be because you’re yielding in the loop. It needs to finish all of your wait()'s before continuing with the next item in the for loop. You can use coroutine.wrap, spawn, or your favorite fastSpawn implementation for an easy fix.

for i,v in pairs(posis:GetChildren()) do
        -- you can wait here without delaying other children.

A more ideal solution might be to go through each child during each step of the process in parallel instead of in series.

1 Like

oops I made a typo, sorry

I meant a few thousand microseconds, not milliseconds;
talking micro-optimization , pairs wasn’t being used correctly for it’s purpose and thus would have taken significantly longer (significantly longer as in by about a hundred to a thousand microseconds).

@httpDerpyy The differences here,

1 Like

I think the dev hub is to blame for improper pairs/ipairs usage. Although pairs will work, ipairs is always preferred for arrays.

1 Like