Return doesn't return

Ciao,
I have a function that should return the i of the for.
Well, it doesn’t. The cycle ends as it should when the user clicks a button, but the return is always nil.

function Vediamo(Opzioni)
	local Clicked = false
	while Clicked == false do
		for i = 1,#Opzioni do
			Opzioni[i].MouseButton1Click:Connect(function()
				Opzioni[i].Parent.Visible = false
				Clicked = true
				return i
			end)
		end
		wait(0.1)
	end
end
local Opzioni = {
				Convo.Opzioni.la1,Convo.Opzioni.la2,Convo.Opzioni.la3
}
local Scelta = Vediamo(Opzioni)
print(Scelta) -- print == nil

Thank you all!

5 Likes

I believe the issue is that you are not defining the options within the function.

1 Like

But it gets whenever i click one of them, and closes the function. But it returns nil?

1 Like

Sorry if I reply incorrectly as I realize you are French so sometimes I might not understand you.

1 Like

It closes the function because it is still returning but it is returning a nil value.

1 Like

I’m Italian :sob: :sob: :sob: :sob: :sob: :sob:
Anyway, I should declare the options up the function?

sorry I didn’t realize you were Italian. Also yes declare the options up the function

If you see, the Vediamo function doesnt return anyting at all, the code

This Line of code is inside the function that is connected to MoueButton1Click Event, so that function will return i, not the Vediamo Function,
and since the function returning i is connected to an event, so no one is using that return. if you want to print every time The Opzioni buttons are getting clicked, you may replace that return i with print(i)
Vediamo function is just connecting events to it, make sure u also call Vediamo function.

Also Please Specify What You Want To Achieve. Thanks!

There is no mousebutton1click event. It’s just a function.

I have declared the options up the function, but it doesn’t help? @betterthenharrybarry

I’m confused, sorry @TheGameDevPro26

Could you show how you tried declaring the options up the function please?

This Is The Line I Am Talking About

1 Like

Oh I realize what you are talking about now. There is a click detector for the options I am assuming and she is trying to return i.

1 Like
--Variabili
local Opzioni = {}

local function Vediamo(Opzioni)
	local Clicked = false
	while Clicked == false do
		for i = 1,#Opzioni do
			Opzioni[i].MouseButton1Click:Connect(function()
				Opzioni[i].Parent.Visible = false
				Clicked = i
				return i
			end)
		end
		wait(0.1)
	end
end

--Funzione
Cartello.MouseClick:Connect(function(Giocatore)
			Opzioni = {
				Convo.Opzioni.la1,Convo.Opzioni.la2,Convo.Opzioni.la3
			} 
			
			local Scelta = Vediamo(Opzioni)
			
			print(Scelta)
end)

yeah, i’m trinna return which button clicked from the vector by it’s position in it.

1 Like
function Vediamo(Opzioni)
	local Clicked = false
	repeat
		for i,v in pairs(Opzioni) do
			v.MouseButton1Click:Connect(function()
				v.Parent.Visible = false
				Clicked = true
				return i
			end)
		end
		wait(0.1)
	until Clicked
end
local Opzioni = {
				Convo.Opzioni.la1,Convo.Opzioni.la2,Convo.Opzioni.la3
}
local Scelta = Vediamo(Opzioni)
print(Scelta) -- print == nil

A few fixes maybe try now? (I kind of just cleaned the code a bit)

Can You Please Mention What You Wanna Acheive? So We Can Help.

Well you do seem to be declaring the options correctly but there is no options in the table. Also I think another issue is that the for loop doesn’t know how much to count by because you are only giving 2 numbers instead of 3.