What does 'return' do?

So how would I restart a while true do statement after it breaks?

1 Like

You can put it in a function, and call the function whenever you need the loop to run again.

For example:

function Add(n1, n2)
  return n1+n2
end

print("2 plus 2 is " .. Add(2,2))

wait(3)

print("Let's call the function again! Hm, what is 4 plus 4 equal to? Oh right, it's " .. Add(4,4))

18 Likes

For the loop to work it requires a player variable. The loop is to make a pet follow the player.

Here’s the script:

    equipPetEvent.OnServerEvent:Connect(function(player, item)
	local equippedPetFolder = player:WaitForChild("EquippedPet")
	local equippedPet = equippedPetFolder:GetChildren()
	local char = player.Character
	local hum = char.Humanoid
	local torso = char.UpperTorso
	
	local maxFloat = 1
	local floatInc = 0.025
    local sw = false
    local fl = 0
	
	for i,v in pairs(equippedPet) do
		v:Destroy()
	end
	
	item:Clone().Parent = equippedPetFolder
	
	local charChildren = char:GetChildren()
	for i,y in pairs(charChildren) do
		if y:FindFirstChild("Pet") then
			y:Destroy()
		end
	end
	
	local petInChar = item:Clone()
	petInChar.Parent = char
	local pet = petInChar.Part
	
	confirmEquipPetEvent:FireClient(player)
	
	
	
	
	while true do
		wait()
		if char:FindFirstChild(item.Name) then
    
    if not sw then
        fl = fl + floatInc
        if fl >= maxFloat then
            sw = true
        end
    else
        fl = fl - floatInc
        if fl <=-maxFloat then
            sw = false
        end
    end
    if pet ~= nil and hum ~= nil and torso ~= nil then
        if hum.Health > 0 then

            local cf = torso.CFrame * CFrame.new(3,2+fl,3)
            pet.BodyPosition.Position = Vector3.new(cf.x,cf.y,cf.z)
            pet.BodyGyro.CFrame = torso.CFrame * CFrame.new(3,0,-3)
  
 else

char[item.Name]:Destroy()
	if not char:FindFirstChild(petInChar.Name) then
		print("Dead")
		wait(6.5)
    workspace:WaitForChild(player.Name)
print("Player respawned")
	local newPet = item:Clone()
	newPet.Parent = player.Character 
	


	end
end
end
end
	end
	end)
4 Likes

It would help if you could post what the aim here is, perhaps after we know that we can propose a better solution or suggest a method you can use to accomplish your goal. Right now all we know is that you want to know the behavior of ‘return’ in a while loop. A while loop will stop when the condition is no longer met.

3 Likes

You mean this?

3 Likes

yes, I want the loop to stop when the player dies. Then respawn the pet and carry on the loop.

‘item’ is the pet

2 Likes

You could use repeat rather than while for this. The script will repeat something until the condition specified is met. In this case you could do:

repeat
   -- Pet following character code here
until not Player.Character -- (Or until player humanoid is dead, etc)

Also this thread is now derailing completely from the original question, and is completely unrelated. In the future before posting in this category read these guidelines.

7 Likes

Example from real life:

You go to a restaurant and order a sandwich and a drink. A bit later, you receive the sandwich and drink. In other words, you gave the restaurant input (your order) and it in turn gave an output (sandwich and drink).

The process of the restaurant outputting would be what return does. In this analogy, you are the caller of the function, and the restaurant is the function.

function Restaurant:OrderFood(orderInfo)
   -- Do some stuff to figure out the actual food
   return food
end

-- You, the customer:
local myFood = Restaurant:OrderFood("Sandwich", "Water")
60 Likes

In general terms, return outputs anything from a scope depending on the input after return.

function test_a()
    return "hi"
end

function test_b(number)
    return number + 2
end

function test_c()
    return workspace:FindFirstChild("foo") -- let's say there was a part in the workspace named "foo"
end


print(test_a()) -- > hi
print(test_b()) -- > your input number in parameter + 2
print(test_c().Name) -- > foo

Simplification: return = output from a scope(if statements, functions, loops(it’ll break the loop))

7 Likes

This could be slightly misleading, I think. A break statement will exit the current scope (if-then, loop, etc) but a return statement will end the currently-executing function and return control to the caller no matter how many levels of loops or conditionals it’s inside. In other words, you can’t use return to jump out of just one level of nested loops, that is what break is for. In Roblox scripts, a return statement that’s not inside a function will terminate the script. In the case of a ModuleScript, the contents of the module is essentially a function, and you normally return something useful, like a table built by the module, or reference to a function within it.

11 Likes

Can also be used organizationally and to end a function prematurely -

function checkobjectsize(object)
     if not object:IsA("BasePart") then return end
     return "Size = "..tostring(object.Size)
end
print(checkobjectsize(game.Workspace.Part))

Will print either nil or the Object’s size and looks better than:

function checkobjectsize(object)
    if object:IsA("BasePart") then
        return "Size = "..tostring(object.Size)
    end
end
3 Likes

At a lower level, returns and breaks are very different. Your Lua code is compiled into a form of byte code similar to machine code that your processor uses. When your script runs, the Lua interpreter reads your byte code like a CPU would read machine code. The Lua interpreter is compiled to machine code and when it reads a byte code from your script, it tells the CPU to execute a section of machine code in the interpreter that effectively performs what the byte code instructed to do. Thus, Lua is called a an interpreted language because it never actually runs directly on the hardware but it describes what needs to be done. The interpreter normally works by reading straight down the bytecode list of instructions that make up your script, one per a “line”. When you use “return”, “break”, “if”, “for”, “while”, or “repeat” then you alter the order in which the interpreter reads your script.

When return and break are translated into byte code, they become into two completely different instructions. Return has its own instruction (just like in x86 machine code / assembly) while break uses the JMP instruction (which has a similar x86 instruction as well).

JMP is a simple instruction that tells the Lua interpreter which instruction to execute next. A JMP instruction tells it to stop reading and start again at a different line.

A RET instruction is a slightly more complex instruction that tells the Lua interpreter to return to the last bookmark. For it to function properly, a bookmark needs to be set using the CALL instruction. To explain how these work, assume that the interpreter is chugging along reading and executing byte code until it reaches a CALL instruction. When it does, it works similarly to a JMP instruction. It stops reading where it was and starts reading where CALL tells it to. While there are more differences, the most important one is that it leaves a bookmark to remember where it was. Depending on how many CALL instructions are encountered, we could have many bookmarks at once. These are stored on what is referred to as “the stack”. The stack functions like a stack of plates at a restaurant: the first plate on the stack is that last plate to be taken off. In similar fashion, a RET instruction tells the interpreter to stop reading where it is and resume reading at the last bookmark on the stack.

Now to make these ideas more concrete. Each function in your code is turned into a single list of instructions. JMP instructions make if statements, loops, and similar language features change which instructions the interpreter reads even though all of the code is just a long list of instructions. A break statement breaks the last loop it was found in by performing a JMP to the first instruction after it. A return statement returns from the function entirely and resumes execution of the code that called it. Here are some examples:

local function foo()
    print '2'
    -- even if you don't put a 'return',
    -- Lua always returns at the end of a function
end

print '1'
foo()
print '3'

This code will print 1, 2, and 3 on different lines in that order. When foo is called, a bookmark is made. When the end of foo is reached, execution returns to that bookmark.

print '1'
for i = 1, 10 do
    print(i)
    if i == 5 then
        break
    end
end
print '11'

This code will print 1, 2, 3, 4, 5, and 11 on different lines in that order. After each print, Lua checks if i == 5. If it is, then it JMPs to the first instruction after the loop, the print '11'. Otherwise, it continues down the list of instruction. In a for loop, it checks if ‘i ~= 10’ at the end. If it doesn’t, then it JMPs back to the beginning of the loop, otherwise allowing the interpreter to fall through to the print '11' statement.

17 Likes

Think of someone throwing you a ball, and asking for it back - so you return the ball.

The same applies in programming, typically relating to math.

If you have used Microsoft Excel before, you’ll understand that when you type in something like;

=SUM(1,2)

it adds 1 and 2 together - returning 3 in the cell.

We can do the exact same with programs

a = 1
b = 2

print(a + b) 
--prints out 3

Different types of values can be returned:

  • Numbers
  • Boolean (true/false)
  • Strings (text)
  • Tables (lists)
  • Objects (posts, models, etc)

In some programming languages, such as C#, you have to define what type of value you want to return.
However Lua is much simpler as it let’s you return whatever you want from a function.

function add(a, b)
    return a + b      -- tell the function to stop here and return the sum of a and b
end

print(add(1, 2)) 
-- prints out 3

local newValue = add(2, 3) -- 5

local anotherValue = add(1, add(2,3) ) -- 6

Have fun.

7 Likes

I feel like this is an XY problem. It’d be better to ask about the actual problem, which seems to be getting your game’s “pets” to float near players and how your current code is faulty, over obscure questions such as “how do you restart a while loop” or “what does return do” (since they’re all unrelated and 3 different questions).

Return can be used as an expression to exit out of the scope or return a value in a callback.

While loops run until their condition is met or the loop is broken. You can’t restart a while loop unless you have something to create it (i.e. a function).

local function SpawnSomeWhileLoop()
    while condition do
        -- stuff
    end
    -- Nothing here runs until the above stops
end

SpawnSomeWhileLoop()
-- Nothing below here runs unless you put the call in a spawn or coroutine

As for your code, I think you’re looking for a different solution. As it is, your code possesses its own respective flaws:

  • There seem to be no server-side checks on the remote, so exploited clients can leverage this remote by passing anything in the “item” argument for use by the server
  • You might want to consider a module or external script that appends logic to the pet, rather than relying on the remote’s connected function to do it
    • The remote should only handle the equipping part of things. An external script or module should be the actual code that acts to give pets their code logic (i.e. floating).
1 Like

You could try coroutines for this. Break and return keywords stop a loop in its tracks, but coroutines have the sort of behavior you want.
When you yield a coroutine it will stop the entire thread/loop, but when you resume it, it will start the function all over again. I recommend going to the roblox wiki to understand the API.

1 Like

Return returns a value when called in a function.
Whenever called it will immediately stop the function and return the given value.
If I’m correct, whenever return is used in a loop it should also stop the loop like break.

function returnstuff(a, b)
 return a + b
end

print(returnstuff(2, 2)) --4


local continue = false

function dostuff()
 print("Start.")
 if not continue then return end --Function will stop here if continue is false.

 print("Function continued.") --This will never happen because return stops the function.
 print("End.")
end

dostuff()


function loopthingy()
 print("Start.")
 for x = 1, 10 do
  print(x)
  if x == 5 then return x end --Will stop the x loop halfway.
 end
 print("End.") --Will never print because return was used inside the loop.
end

print(loopthingy()) --5

Not the best example but maybe this gives a idea of how it works.

4 Likes

When running a while true do loop when you do return it iterates over again.

Edit: I meant if you just simply put ‘return’ with no arguments, I think it works that way judging from my experience.

1 Like

Return doesn’t belong in a while loop, nor is that the function of return. While loops automatically re-run their scopes until the condition is met (while condition do). Typically if you want to terminate a while loop, you should use break. Return doesn’t break the loop, though it exits out of the scope. Exiting out of a while scope causes it to run again should the condition not be met.

I’m not sure what you mean by this, could you provide an example?

Whoooops, excuse me. I went and tested that in Studio, it does not do that. It does exit out of the while scope, but it does not re-run it - it terminates similarly to a break. That being said: return does not cause while loops to re-run, as I initially meant to say.

Fixing my response.

1 Like