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.
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
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
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.
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.
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?)