Make code jump between lines

Hi,
I want to make that the code jumps between lines. For example:

if something == true then
... --makes the code jump to any
end
if something1 = true then
return
end
any --where the code should jump to

But I don’t know how it works. I’ve read online that you can just do.

if something == true then
goto ::any::
end
if something1 = true then
return
end
::any::

But that didn’t work either. So I’m asking for how you would do that in Roblox.

1 Like

Its not the only approach, but you could have functions that run depending on your if statements!

local function one()
--bada bing bada boom functiony stuff
end
local function two()
--keep on keepin' on more function stuff
end

Then

if yes then
one()
else
two()
end
end

goto only works for lines below the line calling it!

goto a
::a:: 

this works, but

::a::
goto a

won’t work! This is where you’d use a funtion !

A:

Python has some special syntax that enables flow control that you could use, as you have referenced. It is called a “label”. You can read more about it in the docs.
::label::

However, this feature is not yet available in ROBLOX. It is expected that in v2.1.0 (soon) those will be made available, you can track the progress on the v2.1.0 github issue.
The other way you could use this is to jump inside an existing function from a Goto statement, like this
unit_test()
local B = 1

print("a")
unit_test()

::START::
if B == 1 then
    print("here")

    B = 0
    goto START
end

print("b")

end

However, this is not recommended. It reflects the way Lua is usually programmed (unlike Python, which is frequently written in a very clean and readable way by avoiding flow control)), and while it might make sense to someone who understands your code, it will only seem like a mess to those who don’t. The above example can just as easily be written like this:
unit_test()
local B = 1

print("a")
unit_test()

print("here")

unit_test()

end

Many languages don’t use this as much as other languages due to its resulting messy logic. To futher explain why you shouldn’t use this:
Might make sense now, but in a larger script template, you will forget what you meants after a month
Very difficult for someone else to understand and improve your code without constantly asking you questions to explain what the code does, since the code isn’t written in a logical, easy to follow order
Anything beyond these 2 lines of ::START:: and if B == 1 then is impossible to have another goto START point to
The code could crash if a mistake is made in something like ::START:: pointing somewhere that there is no ::START:: label for
Feel free to ask for further clarification on anything.

2 Likes