Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you use "raw_input" from Python in RBLX LUA?

Asked by 8 years ago
 In Python, you use raw_input to give a prompt for somebody to answer and returns the data input by the person in a string. I want to do this but, in a roblox game chat box. Like, a bot asks you, "What is your name?" and you answer, "Caelyn". It will respond back saying something like "Hello, Caelyn". I'm sorry if I made this really confusing.

2 answers

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

There is a function of TextBoxes called FocusLost that will fire when the player stops typing in the box (focus shifts elsewhere). It supplies two arguments, if focus was lost due to the player pressing enter and the input object that caused focus to be lost (for this answer, I will only use the first of the two arguments).

Example: Prints out what the player types in the box when the enter key is pressed (with focus of the text box)

local textbox = script.Parent

textbox.FocusLost:connect(function(enterPressed)
    if enterPressed then
        print(textbox.Text)
    end
end)

So now lets write a similar function to that of python's raw_input for Roblox.

We are going to need to connect and disconnect the FocusLost event each time that we call the function, so it may be good to read over the RBXScriptSignal wiki page. So what we are going to do, is make the player focus on the textbox using the CaptureFocus function and then capture what they type using the FocusLost event. We will then disconnect the event and return the result.

I'm going to assume you have both a TextBox and TextLabel (or another TextBox) for user input and for the bot output.

Example: 'raw_input' function

local input = script.Parent.TextBox
local output = script.Parent.TextLabel

local function raw_input(prompt)
    local text = ""
    output.Text = prompt
    input:CaptureFocus()
    local event = input.FocusLost:connect(function(enterPressed)
        if enterPressed and input.Text ~= "" then
            text = input.Text
        else
            input:CaptureFocus()
        end
    end)
    repeat wait() until text ~= ""
    return text
end

local response = raw_input("What is your name?")
output.Text = "Hello, " .. response .. "!"
Ad
Log in to vote
0
Answered by 8 years ago

Don't

In Roblox, you don't have a CLI to wait for input from. Instead, you want to use GUI elements such as TextBoxes, which Players can type into, and then use an event to decide when to read the Text property from the target TextBox.

0
Which is what they stated they wanted to do BlackJPI 2658 — 8y
1
Which is why I told them what they had to do. User#6546 35 — 8y
0
Fair enough BlackJPI 2658 — 8y

Answer this question