I am developing a popup menu in Roblox, however the script isn’t successfully returning the result when the button is clicked. All help is appreciated!
function of ModuleScript being called:
local function AllowConfirm(newMenu)
task.spawn(function()
newMenu.ButtonRow.Confirm.InteractButton.MouseButton1Click:Connect(function()
print("Confirm pressed.")
if newMenu.Options.Selected.Value ~= nil then
print("Returned.")
return newMenu.Options.Selected.Value.Name
else
print("Invalid selection.")
notifier:Notify("Please select an option.", 2, true, true)
end
end)
newMenu.ButtonRow.Cancel.InteractButton.MouseButton1Click:Connect(function()
print("Cancelled.")
return "&cancel"
end)
end)
end
-- This function is inside of a CreateMenu function, but that part functions fine and is unrelated.
-- All other functionality of CreateMenu works flawlessly.
function menu:AllowConfirm()
AllowConfirm(newMenu)
end
Script opening menu:
local module = require(script.Parent)
local menu = module:CreateMenu("example")
menu:SetOptions({"ENABLED","DISABLED"})
menu:Open()
local response = menu:AllowConfirm()
repeat task.wait() until response
print("Response recieved.")
print(response)
menu:Close()
menu:Finish()
The output shows that the ModuleScript is receiving the inputs, and outputs the expected print statements.
you’re returning inside the function wrapped inside task.spawn(), not the parent function of AllowConfirm.
Basically menu:AllowConfirm() will always be returning nil.
You need to yield the code to wait for a response, you should preferably put the repeat task.wait() until response
as part of your AllowConfirm function, and just recognize it as a yielding method.
Something like this:
local function AllowConfirm(newMenu)
local confirmation = nil
newMenu.ButtonRow.Confirm.InteractButton.MouseButton1Click:Connect(function()
print("Confirm pressed.")
if newMenu.Options.Selected.Value ~= nil then
print("Returned.")
confirmation = newMenu.Options.Selected.Value.Name
else
print("Invalid selection.")
notifier:Notify("Please select an option.", 2, true, true)
end
end)
newMenu.ButtonRow.Cancel.InteractButton.MouseButton1Click:Connect(function()
print("Cancelled.")
confirmation = "&cancel"
end)
repeat wait() until confirmation
return confirmation
end
You also need to add a return here (and pass params??), or just change the function I wrote above to be declared as menu:AllowConfirm() and delete these lines.
function menu:AllowConfirm()
return AllowConfirm()
end
After you do all that you can remove repeat task.wait() until response from your bottom code snipet and the code will yield until there’s a response now.