# Returning to the top of a function

Is there a way to make a function go back to the beginning? I found ways to do it with while loops, but can’t figure it out for nonwhile functions.

``````local s
local places = {
place1 = game.Workspace.Destination1.Position,
place2 = game.Workspace.Destination2.Position,
place3 = game.Workspace.Destination3.Position
}
if s ~= lastText then
s = math.random(1,#places)
else
--Don't know how to rerun the function
end

lastText =  s
return places[s]
``````

(everything in this is inside a custom function)

What is the motive of the code?

Basically, get a random destination, and make sure the random destination is not the same as the last picked one.

You can call the function again in the function. This is actually a well-known term in programmed called Recursion. You can also take an iterative approach, but recursion will be easier in this situation.

1 Like

Wait , Can you show me an example? (with code lol)

You literally just call the function again, just replace func with the name of your function.

1 Like

(After s == lastDestination) you can repeat until not(s == last_destination) else just continue?
For example

``````>
> If (s == last_destination) then
> repeat
> --(give s a random value)
>  until not(s == lastdestination)
> else just continue
> end
``````

Weird it does nothing. It does say that the certain function had been ran 4000 times

1 Like

@octanagolamen
It’s hard to do that, or maybe I’m not smart enough.

``````if (s == lastText) then
repeat s = math.random(1,#places) until not (s == lastText)
end
``````

gets “invalid argument #2 to ‘random’ (interval is empty)”

function getDestination()
local s
local places = {
place1 = game.Workspace.Destination1.Position,
place2 = game.Workspace.Destination2.Position,
place3 = game.Workspace.Destination3.Position
}

``````s = math.random(1,#places)
if (s == lastText) then
repeat s = math.random(1,#places) until not (s == lastText)
end

lastText =  s
return places[s]
``````

end

``````local Places = {}
Places.Map1 = "Map1"
Places.Map2 = "Map2"
Places.Map3 = "Map3"

local s ;
local lastplace;

if not(lastplace == nil) then
repeat s = math.random(1,#Places) until not(s == lastplace)
else
s = math.random(1,#Places)
end
lastplace = s
``````

Blockquote

Can you try this script,if it works? I don’t have access to roblox studio rn so I am not being able to test myself.

dang it says the same thing, it happens on the line

else
s = math.random(1,#Places)

thanks for bearing with me so long tho C:

`Places` is a dictionary, not an array. Lua doesn’t count dictionaries with general array stuff. If you can, replace this bit

``````Places.Map1 = "Map1"
Places.Map2 = "Map2"
Places.Map3 = "Map3"
``````

with this

``````Places[1] = "Map1"
Places[2] = "Map2"
Places[3] = "Map3"
``````

This might mean that you need to also change some other code to make it work

3 Likes

Thank you so much lol. I still got an error, but like you said it’s just I need to change some code for when the function gets called. Thank you octane for the help too