Prompt
From codeTank
Contents |
Description
prompt is a function used to prompt the user for input.
Arguments
prompt(text) -- title default to "Prompt" prompt(text, title)
prompt takes one or two arguments. It applies tostring to each argument if it isn't already a string. The first argument is the text displayed above the input field in the window. The optional second argument is the title of the window. If no second argument is provided, the default title is "Prompt".
Returns
prompt will always return one result. If the user closes the window, or clicks the Cancel button, then prompt will return nil. If the user hits Enter, or clicks the OK button, then prompt will return what the user typed in the input field as a string. Please note: if the user types a number, then it will be returned as a string, so you must apply tonumber to it if you are asking for a number.
Example
name = prompt("What is your name?") if (name == nil) then -- if the user closed the window or clicked Cancel alert("Fine. Be a jerk.") return -- exit the script end repeat age = prompt("How old are you, " .. name .. "?", "Hi " .. name) if (age == nil) then -- user closed or canceled alert("Sensative, huh " .. name .. "?") return -- exit the script end age = tonumber(age) if (age == nil) then -- user didn't type a number alert("Please type a number") else if (age < 18) then alert("Gee " .. name .. ", you're young!") elseif (age < 21) then alert("You can't (legally) drink! (in the USA)") elseif (age < 30) then alert("You are cool") else alert("Can't trust you") end return -- exit the script end until false -- loop until the user typed a number

