return will return the object created in the function to the assigned variable. For example, say I wanted to create a function that makes a new part and assigns it a random color. Instead of writing it out everywhere I wanted to make the part, I could just have a function return that part. For example:
function makePart()
local part = Instance.new("Part")
part.Parent = workpace
part.Color3 = Color3.new(math.random())
return part
end
local PartWithRandomColor = makePart()
Getting more complicated:
This is an example of how to use return to get multiple variables. We will get the floor and ceil of a given number (round up and round down)
function Round(number)
local roundUp = math.ceil(number)
local roundDown = math.floor(number)
return number,roundUp,roundDown
end
local OriginalNumber,RoundedUp,RoundedDown = Round(math.random()*math.random(1,100))
print(OriginalNumber.." was rounded up to "..RoundedUp.." and rounded down to "..RoundedDown.."!")
I hope this clears up your question.
2 comments
It just assigns the variable 'joint' on line 2 to the function createJoint(). So if you were to do print(createJoint()), you would get the joint's name printed in the output since the function returned the joint.
im still not understanding