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

a script i found and i have a question i wanna know?

i have seen scripts like

function createJoint(wp0, wp1, wc0x, wc0y, wc0z, wc1x, wc1y, wc1z, name)
	local joint = Instance.new("Motor6D", wp0)
	joint.Part0 = wp0
	joint.Part1 = wp1
	joint.C0 = CFrame.new(wc0x, wc0y, wc0z)
        joint.C1 = CFrame.new(wc1x, wc1y, wc1z)
        joint.Name = name
	return joint
end
**

now when i saw "return joint" i was confused, can someone tell me what does it do when it returns to joint?

2 comments

0

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.

0

im still not understanding

2 answers

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()) --Gives brick a random color
	return part --Returns the part as the assigned variable
end

local PartWithRandomColor = makePart() --Would fire the function and the variable would assign to what it returns

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 --Returns the rounded up and the rounded down version of 	the number
end

local OriginalNumber,RoundedUp,RoundedDown = Round(math.random()*math.random(1,100)) --Puts a random number through the function
print(OriginalNumber.." was rounded up to "..RoundedUp.." and rounded down to "..RoundedDown.."!") --Prints results

I hope this clears up your question.

I can try to explain it but with a simpler example. The return function returns whatever is inputted, as a result from a function.

Here's an example:

function trueOrFalse()
	if math.random(0,1) == 0 then
		return false
	else
		return true
	end
end

print(trueOrFalse()) -- prints either "true" or "false"

In the example the result of the trueOrFalse function will be either true or false, as the name suggests. Now whenever I put trueOrFalse() in my code it will be a more of a value. With the value being either true or false.

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