Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I am a bit confused on “Returning” in Roblox lua, help?

I am confused on returning.

function  iamamemberoftherobloxcommunitysince2015 (isanchored, transperancy)
     local part = game.Workspace.Part
     part.Anchored = isanchored
     part.Transperancy = transperancy
end


iamamemberoftherobloxcommunitysince2015 (true, 0.3)

You see that huh, huh, huh? Yes well umm ahem ahem what if I wanted to return some data ahem ya well... how could I do that.....?

you see my function called “iamamemberoftherobloxcommunitysince2015.” Yeah well comment down below how long you have been a member of Roblox for..... PLS no moderation PLS just wanted to do a fun event on the website....!

6 comments

0

2015 member here, 5 year veteran here, wow 5 years! Just imagine how many years that is, someone could really grow up and change there look in 5 years.

0

You should look on Lua.org about returning

0

you dont need to start a fun event on the website

0

you use it for require scripts

0

@iiMegabyteGaming why not? We could use some excitement on this site ya ”serious scripted boi”

0

@Mega sorry for that sorry I apologize I am very sorry.

4 answers

✓ Accepted answer

So basically "returning" is a function in many other programming languages as well as lua to "return" sometimes, much like function arguments but the other way other, instead of giving argument to a function, you get arguments, for example

local function getCframe(part)
    if part then
        local cframe = part.CFrame
         return cframe
     end
end

local cframe = getCframe(part) -- this variable will have the cframe because we "returned" it

print(cframe) -- this won't be nil

Let's do a better example, say we want to check if a player exists or if you want to multiply or divide numbers

local function checkPlayer(name) -- useful for commands and stuff
      if name then
        for _,v in pairs(game.Players:GetPlayers()) do
            if v.Name:lower():sub(1,#name) == name then
                  local player = v
                  return player
            end
        end

      end
      return nil -- if player doesn't exist we return nil
end)

local function divide(num1,num2)
     if num1 and num2 then
          local result = math.floor(num1 / num2)
           return result
     end
     return nil
end

print(checkPlayer("builderman")) -- isn't in the game so false
print(divide(6,3)) -- prints 2 because we returned the result

I forgot to mention, that return stops a function, if you put anything after it, it will error, this can be useful for like inputbegan function to check if the player is typing:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input,isTyping)
      if isTyping then return end -- if the player is typing, stop the event
end)

Also keep in mind that returning in if statements will only stop the function if the if statement is true

local function check(bool)
       if bool then
           return "hi" -- if bool == true then we will return hi
      end
      -- if it's false then it will still run until another return that either an if statement that's true or no if statement at all
      return "bye"
end

print(test(true)) -- prints hi
print(test(false)) -- prints bye

You can use returning in functions to return something back from where the function was fired.

function SayBruh()
return "Bruh"
end

print(SayBruh())

Bruh That would be your expected output. Hopefully, this example helps you out

Returns in functions make it so that when you call them, you get something out of it. Think of it as paying someone for something. You provide the function its arguments. The function takes those arguments. And then the function gives you something back once its done.

local function add(a,b)
    local sum = a+b
end

print(add(1,5)) --prints nothing cause nothing was returned

However, with a return:

local function add(a,b)
    local sum = a+b
    return sum
end

print(add(1,5)) --prints 6 because you returned a + b

well, you can return datatypes by just putting return at the end before the end). Other advanced concepts are that yes you can return a function. A function is an object. However, the function must be called if you want something to be done. A function that can return a function.... Can be useful in application. Why? because a function returning a function... the enclosing function retains the local variables.

This discussion is preserved as it was. New answers, comments, and votes are closed.