Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I cannot break this function without it saying, "break statement must be inside a loop"?

Asked by 4 years ago

So I am making a computer thing. And it has a background that changes a decal every few seconds.

But whenever I have clicked the GUI Button clicked I want it to break. But there is no way that I know how to because the TicketButton.ClickDetector.MouseClick:Connect(function() is outside of the while true do loop.

Here is my code.

01-- // Varibles
02local ComputerScreen = game.Workspace.Computer.ComputerScreen
03local Decal = game.Workspace.Computer.ComputerScreen.Decal
04local TicketButton = game.Workspace.ComputerScreen.SurfaceGui.TicketButton
05---------------------------------------------------------------------------------
06-- // Script
07while true do
08    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652375883"
09    wait(0.5)
10    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4648289865"
11    wait(0.5)
12    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652374880"
13end
14TicketButton.ClickDetector.MouseClick:Connect(function()
15    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=0"
16    TicketButton.BackgroundTransparency = 0
17    TicketButton.TextTransparency = 0
18end)

But whenever I do,

1TicketButton.ClickDetector.MouseClick:Connect(function()
2    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=0"
3    TicketButton.BackgroundTransparency = 0
4    TicketButton.TextTransparency = 0
5    ``break``
6end)

It errors with a statement, "break statement must be inside a loop".

Is there any way to help me or fix this issue?

Sorry if I make no sense and or this is not even possible, I am a bit new to scripting. Ha! :D

0
You could try to make a variable and do while varname do and instead of doing break just do varname = false birds3345 177 — 4y
0
You cannot break a function with "break". Break is only for loops. However, you can create a variable and whether or not the function will run will depend on its value. AntiWorldliness 868 — 4y

2 answers

Log in to vote
0
Answered by
blazar04 281 Moderation Voter
4 years ago

Simply create a boolean variable such as running, and only run the loop while running is true. Then, you can easily stop the loop by making running false like so:

01-- // Varibles
02local ComputerScreen = game.Workspace.Computer.ComputerScreen
03local Decal = game.Workspace.Computer.ComputerScreen.Decal
04local TicketButton = game.Workspace.ComputerScreen.SurfaceGui.TicketButton
05local running = true
06---------------------------------------------------------------------------------
07-- // Script
08while running do
09    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652375883"
10    wait(0.5)
11    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4648289865"
12    wait(0.5)
13    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652374880"
14end
15TicketButton.ClickDetector.MouseClick:Connect(function()
View all 26 lines...

If you want the loop to start up again when clicked, then instead of saying running = false say running = not running which sets running to the opposite value it was already at.

0
I've gotten my head around what I need to do but it is not working, the decals won't change I don't what the problem is there is no error in the output neither, I've also tried numerous prints to figure out what the issue is and breakpoints which none has helped. Ask if you want my current code. Nicklaus_s 17 — 4y
1
This script will not run anything passed the while loop because your checking infinte times and if a line errors the whole script would not work. You could either add a wait or a coroutine. JesseSong 3916 — 4y
0
He has a wait already, so it should run. But still as Necro_las said below, changing the texture isn't the most efficient. Try doing what he said blazar04 281 — 4y
Ad
Log in to vote
0
Answered by
Necro_las 412 Moderation Voter
4 years ago

Create a marker to check when you press the button like this:

01local clicked = false
02 
03while true do
04    if clicked == false then
05        game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652375883"
06wait(0.5)
07    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4648289865"
08wait(0.5)
09    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652374880"
10    else
11        break
12    end
13end
14 
15TicketButton.ClickDetector.MouseClick:Connect(function()
16    clicked = true
17    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=0"
18    TicketButton.BackgroundTransparency = 0
19    TicketButton.TextTransparency = 0
20end)

And my advise is if ever you want the changing frames to take less prossessing time and effort, instead of changing the Textures, in witch it will have to keep reloading every time, you can leave 3 decals loaded and set properties to visible/invisible.

Answer this question