(Break statement must be inside a loop) when it's clearly inside a loop

I have a script where if a person buys an item in the store it will run the process and then break after it’s done. I want it to break because it will fire multiple times, but I get an error saying the break statement is not in a loop when it clearly is. Here is code:

function addFrames(currentArea, highest)
	for i, Data in ipairs(Loader.ShopLoader) do
		local NextData = NextBag(Data)

		local newLabel = Instance.new("ImageLabel")
		local button = Instance.new("ImageButton")
		local unknown = Instance.new("ImageLabel", button)
		
		if i > highest then
			button.Image = "rbxassetid://9561370900"
			button.Name = "unknown"
			button.ScaleType = "Fit"
			button.BackgroundTransparency = 1
			button.Parent = ContainerHolder.Shop.ScrollingFrame
			
			unknown.Size = UDim2.fromScale(0.5, 0.801)
			unknown.Position = UDim2.fromScale(0.228, 0.075)
			unknown.BackgroundTransparency = 1
			unknown.ScaleType = "Fit"
			unknown.Image = "http://www.roblox.com/asset/?id=10273250504" -- 10333075783
			unknown.Name = "label"
			return
		end
		
		button.Image = "rbxassetid://9561370900"
		button.Name = NextData.bagName
		button.ScaleType = "Fit"
		button.BackgroundTransparency = 1
		button.Parent = ContainerHolder.Shop.ScrollingFrame

		newLabel.Size = UDim2.fromScale(0.5, 0.801)
		newLabel.Position = UDim2.fromScale(0.228, 0.075)
		newLabel.BackgroundTransparency = 1
		newLabel.ScaleType = "Fit"
		newLabel.Image = "rbxassetid://" .. tostring(NextData.ID)
		newLabel.Name = "label"
		newLabel.Parent = button

		Buybtn.MouseButton1Click:Connect(function()
			BuyItem:FireServer(button.Name)
			print("Fired!")
			break
		end)
	end
end

The only part to be worried about is the bottom part. (“Fired”) gets outputted 5 times and no more, no less. I’ve tried a connection and disconnect, yet it still didn’t work.

Any help would be appreciated!

This might be because the loop already ended. What is Loader.ShopLoader?

module.ShopLoader = {
	{bagName  = "starterBagbtn", Cost = 0, ID = "10265590777", Area = "Spawn"},
	{bagName  = "basketBagbtn", Cost = 150, ID = "10265649123", Area = "Spawn"},
	{bagName  = "greenBagbtn", Cost = 500, ID = "10265624834", Area = "Spawn"},
	{bagName  = "yellowBagbtn", Cost = 1250, ID = "10265675347", Area = "Spawn"},
	{bagName  = "greenBag2btn", Cost = 2500, ID = "10265701412", Area = "Spawn"},
	{bagName  = "d", Cost = 1, ID = "10265701411", Area = "Desert"},
	{bagName  = "d", Cost = 1, ID = "10265701411", Area = "Candy"}
}

The last 2 were for testing

1 Like

It may be because its in a connect function, Maybe add a vairiable and when its true break the loop
So in the connect function you would do something like
VairiableNameHere = true
And in the loop you would do
if VairiableNameHere == true then
break
end

That does not work either.

		local breakloop
		
		Buybtn.MouseButton1Click:Connect(function()
			BuyItem:FireServer(button.Name)
			breakloop = true
			
			if breakloop then
				break
			end
		end)

If button.Name is what’s firing, then each name should be unique so the script that’s watching for the event can distinguish each button, right? Maybe that’s why your connection is firing multiple times.

Yes that’s exactly what I was thinking. Since there are 5 items in the shop it will fire 5 times. I’ve had this issue before in other, and I forgot what to do.

The break statement isn’t in a loop, it is in MouseButton1Click event.
I’ll show you example of correct and wrong using of break statement.

Correct statement, script will not give any error.

while task.wait(1) do 
	if something then -- any variable or anything
		break 
	end
end

Wrong statement, where script giving error.

while task.wait(1) do
	ClickDetector.MouseClick:Connect(function() -- your event, in this case when clickDetector is clicked 
		print("Click")
		break
	end)
end

When i was making my game, i had the same issue with breaking loop. I just added variable and when something was did in my game (for example player clicked on part) i setted variable on true and i broke loop. I will give you example of code, because i’m bad at explanation.

You can do the same thing as i mentioned bellow instead of ClickDetector.MouseClick event with MouseButton1Click.

local ClickDetector = workspace.Baseplate.ClickDetector 

local breakLoop = false

while task.wait(0.001) do
	print("sss")
	
	ClickDetector.MouseClick:Connect(function() -- your event, in this case when clickDetector is clicked 
		breakLoop = true
	end)
	
	if breakLoop then
		break 
	end
end
1 Like

Yeah this is not the problem. The problem is what Qurki stated, since there are 5 frames in the shop it will fire 5 times. I tried your code and it did the same thing.

What is highest and nextBag(Data)? They seem to determine button names so that might be where the problem is.

button names are different, but highest is just determining what area the player is in and nextBag(Data) is the data of Loader.ShopLoader.

image

Ok, what about Buybtn? Show it in the explorer.

Butbtn?? What does that mean??

Sorry, I meant Buybtn. ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

image

I think this is getting fired multiple times because there are seven connections due to the pair loop. You need to fix this buy adding some sort of selection system, or multiple buy buttons for each bag.

Couldn’t I just destroy the buy when they click on another item then parent it back

You could, but then you would need to keep setting up connections for the button.

Also, what do you mean by “selection system”?

When a player clicks on a bag, it’s “selected” and then that selected bag name would be fired when the buy button is pressed. (I’m assuming players can select and buy bags they want?)