✓ 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)
print(cframe)
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)
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
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"))
print(divide(6,3))
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
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"
end
return "bye"
end
print(test(true))
print(test(false))
6 comments
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.
You should look on Lua.org about returning
you dont need to start a fun event on the website
you use it for require scripts
@iiMegabyteGaming why not? We could use some excitement on this site ya ”serious scripted boi”
@Mega sorry for that sorry I apologize I am very sorry.