Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Like ipairs, Is there a way to stop a for loop once a child passes through an else condition?

For anyone who never seen my comments on scripting helpers every day for the past week, i am working on a shop script, which allows players to store clothes in a virtual cart, where they can try on the clothing, or buy it later. Currently I am working on getting items to the virtual cart, and to keep from duplicate items getting into the cart, I am trying to filter out clothing items.

What i'm trying to do is to get the script to filter out any duplicate items from the cart. If the script sees no duplicates, then the script would clone and empty item GUI, transfer a clothing ID to the item, and send that GUI over to the cart.

I know that a for loop is involved in this script, but is there a way i can imitate ipairs where once the script sees an item that has a clothing ID, it immediately returns false?

script.Parent.MouseButton1Click:connect(function()
	local CartBox = script.Parent.Parent.Parent.Parent.Parent.Cart.CartGUI
	local Cart = CartBox.CartBody.ItemSelection
	local ID = script.Parent.Parent.Parent.ID.Value
	local Itembox = script.Parent.Parent.Parent.Parent.BoxHold.ItemBox
	local prompt = game.Players.LocalPlayer.PlayerGui.DuplicateItemPrompt
	local image = script.Parent.Parent.Parent.ItemImage.Image 
	if #Cart:GetChildren() == 2 then -- i initially have 2 items in the cart

		CartBox.Visible = true 
		local NewBox = Itembox:Clone()
		NewBox.Parent = Cart
		NewBox.Visible = true
		NewBox.ImageLabel.LogoImage.Image = image
	
		print("ItemBox Cloned")
		NewBox.ImageLabel.ID.Value = ID
		print("Clothing ID transferred to new Item Box")
		
	elseif #Cart:GetChildren() > 2 then
		for i,item in pairs(Cart:GetChildren()) do
			if item :IsA("Frame") then
				if Itembox.ImageLabel.ID.Value ~= ID then
					-- continue for loop
				else
					
					--end for loop
					--open up prompt
					
				end
			end
		end
		
		
	end
	
end)

1 answer

✓ Accepted answer

You could add a Boolean before your loop like so,

elseif #Cart:GetChildren() > 2 then
	local foundDuplicate = false
        for i,item in pairs(Cart:GetChildren()) do
            if item :IsA("Frame") then
                if Itembox.ImageLabel.ID.Value ~= ID then
                   						 -- continue for loop
                else
 			foundDuplicate = true		-- Found a duplicate
	                break				-- Breaks out of loop (maybe not needed)
                end
            end
        end
 	
	if foundDuplicate then
									-- Open up prompt
	else
									-- Do whatever
	end
 
end

1 comment

0

thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.