Returning true before I click the button

I want my buttons to return true or false depending on which one was clicked.
They’re in a module function but when the module function is fired, instead of waiting until they’re clicked, it returns nil and even after theyve been clicked still returns nil.

option1.MouseButton1Click:Connect(function() 
            option1.Visible = false
            option2.Visible = false
            dialoguehead.Enabled = false
            return true
            
        end)
-- 


Roblox’s events don’t return the value, a return inside an RBXConnection only stop the function.

You can make it store in a variable and return the variable:

local value = nil
option1.MouseButton1Click:Connect(function() 
	-- Code
	value = true
end)

If you need the variable to be updated, you can save it in a table and return the table:

local values = {}
option1.MouseButton1Click:Connect(function() 
	-- Code
	values.Active = true
end)

Sorry if it’s obvious, but how would I make the main function (since the whole thing is just in a module function) return true or false based on the ‘value’ variable changing?

It is not the best code but something like this:

return function()
	local Connections = {}
	
	
	
	--		Connect		--
	local Value = nil
	table.insert(Connections, Button_1.MouseButton1Click:Connect(function()
		Value = true
	end))
	table.insert(Connections, Button_2.MouseButton1Click:Connect(function()
		Value = false
	end))
	
	

	--		Timer		--
	local Total = 3
	while Value == nil and Total > 0 do
		Total -= task.wait()
	end
	
	--		Disconnect		--
	for _, v in pairs(Connections) do
		v:Disconnect()
	end
	return Value
end

So, the code will connect the events and wait 3 seconds (or less if you click) to return the value.

Return nil if you don’t click, basic function but you can adapt for what you need.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.