Module func not return

Good afternoon, right away I want to apologize for my English, I do not know it well because there is no training in Ukraine for 2 years.

I have this problem, I created a module to show the gui of the question with the answer in the form of a button “yes or no”

The module after the call in a normal script should return the user’s answer, but returns null because it finishes its work without waiting for the player’s answer.

Here is a piece of code from the module:

yesButton.MouseButton1Up:Connect(function()
		yesButton:TweenSize(originalSize, Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.1, true)
		button1UpSound:Play()
		wait(0.1)
		animationModule.closeFrame(frame)
		
		return true
	end)
	
	noButton.MouseButton1Up:Connect(function()
		noButton:TweenSize(originalSize, Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.1, true)
		button1UpSound:Play()
		wait(0.1)
		animationModule.closeFrame(frame)
		
		return false
	end)

A call in a regular script:

local result = messageModule.boxQuestionMessage(player, "Do you want to buy this area?")
		print(result)

The return statements will return the value to the connection’s handler function which is discarded since only the engine can observe that value and it doesn’t need any return values. Something you may want to do instead is use a signal (pure Luau signal or BindableEvent) and have your buttons fire that signal with the response, then have your code wait on the signal.

Some silly disjointed examples:

Button1.MouseButton1Up:Connect(function ()
    answer:Fire(true)
end)

Button1.MouseButton1Up:Connect(function ()
    answer:Fire(false)
end)

function module.boxQuestionMessage()
    return answer.Event:Wait()
end

For synchronous code that you want results for, probably the way you want to go though is a BindableFunction moreso than an event. Take a look at both and see what fits your needs.

Is Remote Event a good option?

If the server is asking the client the question and you need the client to respond to the server with the option it picked, yes. I said bindables since I assumed that wasn’t the case, but if another environment is involved then yeah you’ll need remotes.

1 Like

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