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 |
02 | local ComputerScreen = game.Workspace.Computer.ComputerScreen |
03 | local Decal = game.Workspace.Computer.ComputerScreen.Decal |
04 | local TicketButton = game.Workspace.ComputerScreen.SurfaceGui.TicketButton |
05 | --------------------------------------------------------------------------------- |
06 | -- // Script |
07 | while 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" |
13 | end |
14 | TicketButton.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 |
18 | end ) |
But whenever I do,
1 | TicketButton.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 `` |
6 | end ) |
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
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 |
02 | local ComputerScreen = game.Workspace.Computer.ComputerScreen |
03 | local Decal = game.Workspace.Computer.ComputerScreen.Decal |
04 | local TicketButton = game.Workspace.ComputerScreen.SurfaceGui.TicketButton |
05 | local running = true |
06 | --------------------------------------------------------------------------------- |
07 | -- // Script |
08 | while 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" |
14 | end |
15 | TicketButton.ClickDetector.MouseClick:Connect( function () |
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.
Create a marker to check when you press the button like this:
01 | local clicked = false |
02 |
03 | while true do |
04 | if clicked = = false then |
05 | game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652375883" |
06 | wait( 0.5 ) |
07 | game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4648289865" |
08 | wait( 0.5 ) |
09 | game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652374880" |
10 | else |
11 | break |
12 | end |
13 | end |
14 |
15 | TicketButton.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 |
20 | end ) |
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.