No you can’t do that.
You need to nest it in the loop for it to work or else the program has no idea what you’re talking about
However, if you nested it correctly it would work.
Breaks work in practically any instance.
local button = script.parent
while true do
if "Something" then
local function onButtonActivated()
end
end
end
button.Activated:Connect(onButtonActivated)
"Something" will always evaluate true, since it is an existing variable.
Also, you still didn’t use a break, meaning that the loop will still yield.
PLUS, your function is also declared out of scope.
Personally, i’d fix it like this:
local button = script.parent
local val = "Something"
while true do
if val then
local function onButtonActivated()
end
button.Activated:Connect(onButtonActivated)
break
end
end
local runing = true
someSignal:Connect(function() -- the signal will stop the loop
runing = false
end)
while runing do
-- the code will run while runing == ture
end
local button = script.parent
local val = "Something"
while true do
if val then
break
end
end
local function onButtonActivated()
if val then
-- Do something
end
end
button.Activated:Connect(onButtonActivated)
Also, you need to add some yield / wait using task.wait() to your loop outside of the if statement, or else your script will crash.
And if val is true, then we run the code inside of the function that is connected to the event whenever the event fires. It does the same thing. But from my own experience it’s generally not good practice to initiate events / functions inside of loops.