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]
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.
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]
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.
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