Need to understand better what return does

local function chooseRandom()
	local randomPart = script.Parent.Plates:GetChildren()[math.random(1, #script.Parent.Plates:GetChildren())]
	if randomPart.Transparency == 0 then
		return randomPart
	else
		print("picking something else")
		chooseRandom()
	end
end


function test()
	print("gives part is '".. chooseRandom().Name.. "'")
end

while task.wait(0.1) do
	test()
end

okay so I have this little code just to show you what I’m talking about. In it in my theory test() should call a function which would repeatedly call itself if criteria isn’t reached. But for some reason even like that chooseRandom() or test() still picks something up even if criteria isn’t reached, which leads to errors

as you can see on the screenshot all the prints work but the error tells us that it for some reason gathered something from the function. Anyone can explain why? Or any workaround?

I see your problem.

When you first call choose random without picking anything else, it returns the function to the print statement. So, it works.

However, when you call choose random and it picks something else the function is actually called from the else statement through recursion.

You might want to add more functionality in cases like that.

Feel free to correct me if I’m wrong!

Youre setting chooserandom.Name regardless of the output.

try

if chooseRandom() then
 print("gives part is '".. chooseRandom().Name.. "'")
end

that way it doesnt force index the function when nothing is returned or when its nil

You need to return the chooseRandom function when you pick a new item.

local function chooseRandom()
	local randomPart = script.Parent.Plates:GetChildren()[math.random(1, #script.Parent.Plates:GetChildren())]
	if randomPart.Transparency == 0 then
		return randomPart
	else
		print("picking something else")
		return chooseRandom()
	end
end

tl:dr:
return just return’s a value to the line that executed the function

Well to start off to fix your error you can always do something like this.

print(`given part is  {chooseRandom().Name} `)

The difference is how you manipulate the string

print("gives part is '".. chooseRandom().Name.. "'")

Using this method of string manipulation allows ease of access when adding in values. I belive its called String interpolation

As for what does return do. You’re on the right track with the code.

It returns a value to what ever called the function.

So a good example would be something like this

local function GetRandomNumber()
	return math.random(1,100)
end

print(GetRandomNumber())

What will happen is each time your start the game it will print the random number that was gotten from the function.

You can also save the return value as a varible something like this.

local function GetRandomNumber()
	return math.random(1,100)
end

local RandomNumber = GetRandomNumber()
print(RandomNumber)
task.wait(1)
print(RandomNumber)

This way anytime you print or use the variable “RandomNumber” It will stay the same.

Another fun fact is you can check the return to see if the value is valid before running code. You can also attach extra values on return. I personally do this when working with moduals. Here is an example

local function GetRandomNumber()
	local Number =  math.random(1,2)
	if Number == 1 then 
		return Number 
	else 
		return false, "I am a error message"
	end
end

local RandomNumber, ErrorMessage = GetRandomNumber()
if RandomNumber then
	print(`Random number was a 1`)
elseif not RandomNumber then
	print(`Number was not infact 1 {ErrorMessage}`)
end

This will then print out something like
840a816ba6172463cc7cfa1a1298b739

This is a good way to prevent errors on returns or check if certain values are met before running code. Hope this helped any :smiley: