xcx_derek
(DerekEternity)
October 13, 2021, 10:13pm
#1
How would I return a loop to its start?
Example, what if I wanted this code to go back to line 1 rather than continue normally?
local Amount = 1
while task.wait() do
Amount += 1
if Amount == 5 then
print("Reached 5")
Amount = 0
--Return to start somehow
end
end
7z99
(cody)
October 13, 2021, 10:15pm
#2
You’re looking for a function:
local function doStuff()
local Amount = 1
while task.wait() do
Amount += 1
if Amount == 5 then
print("Reached 5")
Amount = 0
--Return to start somehow
doStuff()
end
end
end
doStuff()
Also if you’re looking to do something infinitely, you can do something like this:
while true do
local amount = 1
while true do
amount += 1
if amount == 5 then
break
end
task.wait()
end
end
xcx_derek
(DerekEternity)
October 13, 2021, 10:18pm
#3
Thanks a lot that makes sense, I don’t know how I didn’t think about that earlier
1 Like
Also return is an easier way.
7z99
(cody)
October 13, 2021, 10:20pm
#5
Wdym? return
would just stop the thread.
xcx_derek
(DerekEternity)
October 13, 2021, 10:22pm
#6
Return doesn’t do that. It returns a value and stops a thread i.e.
local function blah()
return "Hi"
end
print(blah())
--Output: Hi
TheDCraft
(TheDCraft)
October 13, 2021, 10:25pm
#7
while true do
amount = 1
repeat task.wait()
amount += 1
until amount == 5
end
Just another way to do it. (I think)
1 Like
My bad, a little rusty in the lua department, also having come from other languages.
D0RYU
(nici)
October 13, 2021, 10:28pm
#9
here is a link about recursion, it basically talks about what Cody showed
thought this might be helpful
1 Like
xcx_derek
(DerekEternity)
October 13, 2021, 10:51pm
#10
thanks but i already know how recursion works, i was asking how to return to the start of a loop
1 Like
D0RYU
(nici)
October 13, 2021, 10:55pm
#11
I know what the question was, I just sent the link incase you didn’t know much about it
my bad
1 Like