For Loop not running twice in a function

So, I am working on an ordering system (https://www.candsavor.com). I am trying to clone a template into two different frames but it does not seem to be cloned and parented to both frame. It works for one frame. Here’s the script:

game.ReplicatedStorage:WaitForChild("CandsavorEvents"):WaitForChild("addToCart").OnServerEvent:Connect(function(plr, quantity, name, description, imageid)
	for i = 1, quantity do
		local Clone = script.CartSlotTemplate:Clone()
		Clone.Parent = plr.PlayerGui.KioskUI.MainFrame.Cart.Cart

		Clone._Name.Text = name
		Clone._Description.Text = description
		Clone._Icon.Image = imageid
	end

	for i = 1, quantity do
		local AnotherClone = script.CartSlotTemplate:Clone()
		AnotherClone.Parent = plr.PlayerGui.KioskUI.MainFrame.Home.Cart

		AnotherClone._Name.Text = name
		AnotherClone._Description.Text = description
		AnotherClone._Icon.Image = imageid
	end
end)

It parents the clone into plr.PlayerGui.KioskUI.MainFrame.Cart.Cart but it doesn’t into plr.PlayerGui.KioskUI.MainFrame.Home.Cart. I tried printing and see if the second loop runs, and it actually ran. Not sure why cloning isn’t working, pretty odd. It doesn’t give any error either.

UI

:green_circle: = Frames i want the TemplateSlot to be parented to.
image

2 Likes

Is that an actual script or local script and where is it parented also i belive its already clone because you didnt set its position so its gonna be at the same spot thus you wont notice if its clone

Do you have you’re console open?
Maybe it’s not running twice because at the first loop it found an error that shut down the code.

As I mentioned, it did not give any error. And I also tried printing on the second loop, and it actually printed. Which means, the second loop runs, it just does not clone.

When you say about first and second loop

You mean that this code has to run twice?

Or this is the second loop?

On a side note you can make the two copies in a single loop.

Yup, exactly. Those two loops does to same thing, just parents in different frames.

@blokav I tried that, that did not work either. It would only clone and parent to the First directory, not plr.PlayerGui.KioskUI.MainFrame.Home.Cart

Try adding those prints in the first code:

print(Clone.Name)
print(Clone.Parent)
print(i)
(before space)

(after space)
print(Clone.Parent)
print(Clone.Name)
print("First Code")

Do the same thing for the 2nd code but change Clone with AnotherClone and First Code with Second Code, then run the game and tell us what was written on the console.
I may have an idea of what to do.

Code ran
game.ReplicatedStorage:WaitForChild("CandsavorEvents"):WaitForChild("addToCart").OnServerEvent:Connect(function(plr, quantity, name, description, imageid)
	for i = 1, quantity do		
		local Clone = script.CartSlotTemplate:Clone()
		Clone.Parent = plr.PlayerGui.KioskUI.MainFrame.Cart.Cart
		
		print(Clone.Name)
		print(Clone.Parent)
		print(i)
		
		Clone._Name.Text = name
		Clone._Description.Text = description
		Clone._Icon.Image = imageid
		
		print(Clone.Name)
		print(Clone.Parent)
		print(i)
		

		print(Clone.Parent)
		print(Clone.Name)
		print("First Code")
	end
	
	for i = 1, quantity do
		local AnotherClone = script.CartSlotTemplate:Clone()
		AnotherClone.Parent = plr.PlayerGui.KioskUI.MainFrame.Home.Cart
		
		print(AnotherClone.Name)
		print(AnotherClone.Parent)
		print(i)
		
		AnotherClone._Name.Text = name
		AnotherClone._Description.Text = description
		AnotherClone._Icon.Image = imageid
		
		print(AnotherClone.Parent)
		print(AnotherClone.Name)
		print("Second Code")
	end
end)
Output
  20:25:26.761  CartSlotTemplate  -  Server - CandsavorHandler:217
  20:25:26.762  Cart  -  Server - CandsavorHandler:218
  20:25:26.762  1  -  Server - CandsavorHandler:219
  20:25:26.762  CartSlotTemplate  -  Server - CandsavorHandler:225
  20:25:26.763  Cart  -  Server - CandsavorHandler:226
  20:25:26.763  1  -  Server - CandsavorHandler:227
  20:25:26.763  Cart  -  Server - CandsavorHandler:230
  20:25:26.763  CartSlotTemplate  -  Server - CandsavorHandler:231
  20:25:26.764  First Code  -  Server - CandsavorHandler:232
  20:25:26.764  CartSlotTemplate  -  Server - CandsavorHandler:217
  20:25:26.765  Cart  -  Server - CandsavorHandler:218
  20:25:26.765  2  -  Server - CandsavorHandler:219
  20:25:26.765  CartSlotTemplate  -  Server - CandsavorHandler:225
  20:25:26.765  Cart  -  Server - CandsavorHandler:226
  20:25:26.765  2  -  Server - CandsavorHandler:227
  20:25:26.766  Cart  -  Server - CandsavorHandler:230
  20:25:26.766  CartSlotTemplate  -  Server - CandsavorHandler:231
  20:25:26.766  First Code  -  Server - CandsavorHandler:232
  20:25:26.767  CartSlotTemplate  -  Server - CandsavorHandler:217
  20:25:26.767  Cart  -  Server - CandsavorHandler:218
  20:25:26.767  3  -  Server - CandsavorHandler:219
  20:25:26.768  CartSlotTemplate  -  Server - CandsavorHandler:225
  20:25:26.768  Cart  -  Server - CandsavorHandler:226
  20:25:26.768  3  -  Server - CandsavorHandler:227
  20:25:26.769  Cart  -  Server - CandsavorHandler:230
  20:25:26.769  CartSlotTemplate  -  Server - CandsavorHandler:231
  20:25:26.769  First Code  -  Server - CandsavorHandler:232
  20:25:26.770  CartSlotTemplate  -  Server - CandsavorHandler:239
  20:25:26.770  Cart  -  Server - CandsavorHandler:240
  20:25:26.771  1  -  Server - CandsavorHandler:241
  20:25:26.771  Cart  -  Server - CandsavorHandler:247
  20:25:26.771  CartSlotTemplate  -  Server - CandsavorHandler:248
  20:25:26.771  Second Code  -  Server - CandsavorHandler:249
  20:25:26.772  CartSlotTemplate  -  Server - CandsavorHandler:239
  20:25:26.773  Cart  -  Server - CandsavorHandler:240
  20:25:26.773  2  -  Server - CandsavorHandler:241
  20:25:26.773  Cart  -  Server - CandsavorHandler:247
  20:25:26.774  CartSlotTemplate  -  Server - CandsavorHandler:248
  20:25:26.774  Second Code  -  Server - CandsavorHandler:249
  20:25:26.775  CartSlotTemplate  -  Server - CandsavorHandler:239
  20:25:26.775  Cart  -  Server - CandsavorHandler:240
  20:25:26.775  3  -  Server - CandsavorHandler:241
  20:25:26.776  Cart  -  Server - CandsavorHandler:247
  20:25:26.776  CartSlotTemplate  -  Server - CandsavorHandler:248
  20:25:26.776  Second Code  -  Server - CandsavorHandler:249
  20:25:29.179  CartSlotTemplate  -  Server - CandsavorHandler:217
  20:25:29.179  Cart  -  Server - CandsavorHandler:218
  20:25:29.179  1  -  Server - CandsavorHandler:219
  20:25:29.180  CartSlotTemplate  -  Server - CandsavorHandler:225
  20:25:29.180  Cart  -  Server - CandsavorHandler:226
  20:25:29.180  1  -  Server - CandsavorHandler:227
  20:25:29.180  Cart  -  Server - CandsavorHandler:230
  20:25:29.180  CartSlotTemplate  -  Server - CandsavorHandler:231
  20:25:29.181  First Code  -  Server - CandsavorHandler:232
  20:25:29.181  CartSlotTemplate  -  Server - CandsavorHandler:217
  20:25:29.181  Cart  -  Server - CandsavorHandler:218
  20:25:29.181  2  -  Server - CandsavorHandler:219
  20:25:29.182  CartSlotTemplate  -  Server - CandsavorHandler:225
  20:25:29.182  Cart  -  Server - CandsavorHandler:226
  20:25:29.182  2  -  Server - CandsavorHandler:227
  20:25:29.182  Cart  -  Server - CandsavorHandler:230
  20:25:29.183  CartSlotTemplate  -  Server - CandsavorHandler:231
  20:25:29.183  First Code  -  Server - CandsavorHandler:232
  20:25:29.184  CartSlotTemplate  -  Server - CandsavorHandler:217
  20:25:29.184  Cart  -  Server - CandsavorHandler:218
  20:25:29.184  3  -  Server - CandsavorHandler:219
  20:25:29.185  CartSlotTemplate  -  Server - CandsavorHandler:225
  20:25:29.185  Cart  -  Server - CandsavorHandler:226
  20:25:29.185  3  -  Server - CandsavorHandler:227
  20:25:29.185  Cart  -  Server - CandsavorHandler:230
  20:25:29.186  CartSlotTemplate  -  Server - CandsavorHandler:231
  20:25:29.186  First Code  -  Server - CandsavorHandler:232
  20:25:29.187  CartSlotTemplate  -  Server - CandsavorHandler:239
  20:25:29.187  Cart  -  Server - CandsavorHandler:240
  20:25:29.187  1  -  Server - CandsavorHandler:241
  20:25:29.187  Cart  -  Server - CandsavorHandler:247
  20:25:29.188  CartSlotTemplate  -  Server - CandsavorHandler:248
  20:25:29.188  Second Code  -  Server - CandsavorHandler:249
  20:25:29.188  CartSlotTemplate  -  Server - CandsavorHandler:239
  20:25:29.188  Cart  -  Server - CandsavorHandler:240
  20:25:29.189  2  -  Server - CandsavorHandler:241
  20:25:29.189  Cart  -  Server - CandsavorHandler:247
  20:25:29.189  CartSlotTemplate  -  Server - CandsavorHandler:248
  20:25:29.189  Second Code  -  Server - CandsavorHandler:249
  20:25:29.190  CartSlotTemplate  -  Server - CandsavorHandler:239
  20:25:29.190  Cart  -  Server - CandsavorHandler:240
  20:25:29.190  3  -  Server - CandsavorHandler:241
  20:25:29.190  Cart  -  Server - CandsavorHandler:247
  20:25:29.190  CartSlotTemplate  -  Server - CandsavorHandler:248
  20:25:29.190  Second Code  -  Server - CandsavorHandler:249

I’ve read the whole thing, and it seems pretty normal.
Try running it again and click on the print parent so you can see where it leads you to, maybe you wrote the wrong directory and it’s being cloned to somewhere else.
Are you firing the event twice? Maybe this is messing up with the script.
Are the frames visible true or false? If you’re supposed to see the frame try checking if it’s visible.

I still couldn’t find a way out. And yes, I have tried all of them - in explorer it doesn’t add the Template to the following parent meaning it’s not issue. What I will do now is, use ChildAdded and ChildRemoved event for the frame that actually works fine, and clone it locally in a different local script. Thanks for your help!

1 Like

No problem, glad you’ve found a solution to your script!

1 Like