-- Simple introduction into returning --

function Func(x, y)
print x + y 
return x * y
end


1. Variable
local FuncRun = Func(2, 4)
print(FuncRun)

>> 6
>> 8


2. Print
print(Func(2, 4))

>> 6
>> 8function factorial(x)
  if x == 1 then
    return 1
  end
  return x * factorial(x-1)
end
--if return is confusing like it was for me let me tell you
--how it became easy for me.
--see this
  
function Name()
return "Ali"
end

local name = Name()
print(name)
--it should print Ali
--that how return in lua works you Call a function as a Veriable
--that Veriable will Catch the return know that in lua functions
--yield a thread that means line 8 will yield until the Name() function has
--finished executing thats what i call that function dead / we cant
--resume it anymore. i dont wanna get into coroutines just know a thread /
--function is dead when arriving to end / return or an error line.
      1
--here is a very usefull example:
      
function MagnetudeCalculator(objvector3, targetvector3)
    local magnitude, success, exeption
        
    if objvector3 == nil then -- we check if objvector3 is nil
    	magnitude, success, exeption = nil, false, "Vector3 Expected Not Nil!"
       return magnitude, success, exeption   -- this will return nil, false, "Vector3 Ex..."
    end
        
    if targetvector3 == nil then -- we check if targetvector3 is nil
    	magnitude, success, exeption = nil, false, "Vector3 Expected Not Nil!"
        return magnitude, success, exeption   -- this will return nil, false, "Vector3 Ex..."
    end
        
    magnitude, success, exeption = (objvector3 - targetvector3).Magnitude, true, "No Errors"
    return magnitude, success, exeption 
end

-- here we Call the function from diffrent ways to get diffrent results
local magnetude, success, exeption = MagnetudeCalculator(Vector3, Vector3)
print(magnetude, success, exeption) -- some numbers, true, No Er...

local magnetude, success, exeption = MagnetudeCalculator(Vector3)
print(magnetude, success, exeption) -- nil, false, Vector3 Ex...

local magnetude, success, exeption = MagnetudeCalculator(nil, Vector3)
print(magnetude, success, exeption) -- nil, false, Vector3 Ex...

-- just like you guys see return is very usefull to use
-- you could also use return just like that to end a function like:
function Fun()
  
	if 1+1 == 2 then
    
    	return -- the function ends / function is dead and cannot be resumed
    
    end
  
end

Fun()-- the script has gone back to Fun() and will come back when Fun() ends or when the function reaches return
print("Fun Has Ended!")-- this will print when Fun has reached end or return

-- the function wont reach end because its already returned so this whole 
-- prosses could be very confusing just know that if u use return
-- nothing behind the function resumes but it will go back to the caller
-- see all this proccess like a very fast marker the marker will
-- mark everything but not functions until the function gets called
-- then the marker will go through and executes the function
-- and when return is used the marker will go back to where
-- the function was called that could be anywhere in the script
-- or mabey a diffrent script if you are not using the same
-- script for the call it doesnt matter return will always find a way
-- just be carefull what you send because im not a profissional
-- but there may be limits to return veriables check the official
-- lua website for this ill attach a link down below
-- and know break is not the same as return break
-- is used to break out of a loop an continue the function
-- / script but when a return is used in a loop the loop and
-- function will be dead that means the function will not be
-- executed further instead it will be returned to the sender

function LoopBreak()
  
  	while true do
    
		local i = 0

    	if i == 0 then
      
      	break
      	
      	end
  	end
  
  	print("Broke") -- this should print out
end

function LoopReturn()
  
  	while true do
    
		local i = 0

    	if i == 0 then
      
      	Return
      	
      	end
  	end
  
  	print("Broke") -- tthis will never be printed
end

LoopBreak()

-- lastely what i mean by dead i mean that the function can always
-- be called again but it will never be resumed in case of coroutines
-- coroutines are hard to master and more in the advanced way of
-- lua programming they have thier own rules and complications
-- like always coroutines are also pretty usefull
-- just to tell u guys a brief about coroutines
-- coroutines can be yielded, resumed, and the most benifits from
-- using coroutines they dont yield a thread but make thier own thread

-- all credits goes to ME!

Lua相关代码片段

Lua Time.os, Live-Time in seconds

xmake add package

lua string.find

MACAddress Parsing Lua

get three random items out of a table lua

fivem spawn vehicle

on button click roblox

convert table to string

lua base64 encode and decode

how to load character in roblox studio

lua copy table values

how to print what type of obj in roblox lua

roblox custom chat commands

tweenservice

linguagem Lua codigo

how to set line number for nvim in lua

mpv plugin directory

roblox studio get player age

registercommand fivem

esx import fivem

remove space from string roblox lua

lua ternary operator

How to get closest player (FiveM Lua)

roblox studio scale to offset

lua check if string is empty

Embedding Lua in Javascript

lua if not

lua comment

roblox sphere volume formula

Expecting end on while loop got lua

how to find pi lua

how to set a countdown in lua

how hack roblox

roblox check if hit part is part of a player

Wait function lua

how to program lua roblox variables

Roblox lua

roblox loop animation

roblox if statement

roblox if then

Lua string

roblox remove vectorForce friction

fivem show help text with key

how to access mathf module luatoolbox

lua move element in table from last to first

How To Script A Dice Lua

Tostring and tonumber in lua

Ledge Climbing

fivem include mysql-async

how to make a variable for a leaderstat with lua

LUA how to clone

Lua When part touches : Delete()

how to make a variable roblox

Data Saving Script Roblox

how to make a function in lua?

nvim init.lua paht

how to make a vector3 roblox

lua declare inf value

roblox disable zoom mouse scroll wheel lua

esx check if user has item

lua variable

how to get avatar worth

inline variables lua

lua odd or even

roblox value changed connect

roblox studio detect if camera is moving

how do i use the love button module lua assist

Lua script

how do i access the love button module lua assist

fivem server id of nearest player

how do i use the love genf module lua assist

FiveM check if vehicle has flat tires

roblox lua server function on interval continuous

fivem registerkeyMapping

roblox string format zero padding

lua returning

gpsnsnpndmggpsnsnpndmgsrggmmpprrrr

lua global functions

lua graphical interface

roblox lua print string format