Module keeps returning Nil

Hello There

I am trying to make a confirm action system using Module scripts.

The module keeps returning nil, even tho i want it to return a yes or no

Module Script:

local module = {}

local frameAnimation = require(script.Parent.FrameAnimations)

function module:PopUp(plr, title, desc)
	if plr then
		local playerUi = plr:WaitForChild("PlayerGui")
		local popup = playerUi:WaitForChild("MainGui"):FindFirstChild("Confirm")
		
		popup.title.Text = title
		popup.TextFrame.text.Text = desc
		
		if popup.Size == UDim2.new(0,0,0,0) then
			frameAnimation:Start(plr, popup, UDim2.new(0.289, 0,0.507, 0))
		end	 
		
		popup.yes.MouseButton1Click:Connect(function()
			if popup.Size ~= UDim2.new(0,0,0,0) then
				frameAnimation:Start(plr, popup, UDim2.new(0.289, 0,0.507, 0))
			end
			return true
		end)
		
		popup.no.MouseButton1Click:Connect(function()
			if popup.Size ~= UDim2.new(0,0,0,0) then
				frameAnimation:Start(plr, popup, UDim2.new(0.289, 0,0.507, 0))
			end
			return false
		end)
	end
end 

return module

Test Script:

local module = require(game.ReplicatedStorage.GameUi.Modules.ModuleScript)

game.Players.PlayerAdded:Connect(function(plr)

local response = module:PopUp(plr, "TITLE", "HELlo dude, HELlo dude, HELlo dude, HELlo dude, HELlo dude,")

print(response)

end)

Any help with this system would be appreciated

Thanks Fourthbyname_2

1 Like

Event connections do not yield code until a response is ready. You have to do that yourself by using a variable for the response(result) and constantly waiting until its set to true or false:

function module:PopUp(plr, title, desc)
	if plr then
		local playerUi = plr:WaitForChild("PlayerGui")
		local popup = playerUi:WaitForChild("MainGui"):FindFirstChild("Confirm")

		popup.title.Text = title
		popup.TextFrame.text.Text = desc

		if popup.Size == UDim2.new(0,0,0,0) then
			frameAnimation:Start(plr, popup, UDim2.new(0.289, 0,0.507, 0))
		end	 
		
		local result = nil
		
		local yes
		yes = popup.yes.MouseButton1Click:Connect(function()
			if popup.Size ~= UDim2.new(0,0,0,0) then
				frameAnimation:Start(plr, popup, UDim2.new(0.289, 0,0.507, 0))
			end
			yes:Disconnect() --always disconnect events that are supposed to be fired once.
			result = true 
		end)
		
		local no
		no = popup.no.MouseButton1Click:Connect(function()
			if popup.Size ~= UDim2.new(0,0,0,0) then
				frameAnimation:Start(plr, popup, UDim2.new(0.289, 0,0.507, 0))
			end
			no:Disconnect() --same here
			result = false
		end)
		
		--DO NOT USE (until result) it will ignore false and break the script
		--basically we wait until a reply is given
		repeat task.wait(.2) until result ~= nil 
	end
end 
2 Likes

Thanks for the response. This looks like it’s going to work. You are a legend :slightly_smiling_face:

2 Likes