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.

-- // Varibles
local ComputerScreen = game.Workspace.Computer.ComputerScreen
local Decal = game.Workspace.Computer.ComputerScreen.Decal
local TicketButton = game.Workspace.ComputerScreen.SurfaceGui.TicketButton
---------------------------------------------------------------------------------
-- // Script
while true do
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652375883"
    wait(0.5)
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4648289865"
    wait(0.5)
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652374880"
end
TicketButton.ClickDetector.MouseClick:Connect(function()
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=0"
    TicketButton.BackgroundTransparency = 0
    TicketButton.TextTransparency = 0
end)

But whenever I do,

TicketButton.ClickDetector.MouseClick:Connect(function()
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=0"
    TicketButton.BackgroundTransparency = 0
    TicketButton.TextTransparency = 0
    ``break``
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

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:

-- // Varibles
local ComputerScreen = game.Workspace.Computer.ComputerScreen
local Decal = game.Workspace.Computer.ComputerScreen.Decal
local TicketButton = game.Workspace.ComputerScreen.SurfaceGui.TicketButton
local running = true
---------------------------------------------------------------------------------
-- // Script
while running do
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652375883"
    wait(0.5)
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4648289865"
    wait(0.5)
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652374880"
end
TicketButton.ClickDetector.MouseClick:Connect(function()
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=0"
    TicketButton.BackgroundTransparency = 0
    TicketButton.TextTransparency = 0
end)

TicketButton.ClickDetector.MouseClick:Connect(function()
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=0"
    TicketButton.BackgroundTransparency = 0
    TicketButton.TextTransparency = 0
    running = false
end)

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:

local clicked = false

while true do
    if clicked == false then
        game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652375883"
wait(0.5)
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4648289865"
wait(0.5)
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=4652374880"
    else
        break
    end
end

TicketButton.ClickDetector.MouseClick:Connect(function()
    clicked = true
    game.Workspace.Computer.ComputerScreen.Decal.Texture = "http://www.roblox.com/asset/?id=0"
    TicketButton.BackgroundTransparency = 0
    TicketButton.TextTransparency = 0
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.

Answer this question