Is there a way of getting input from the console in Roblox?

Vanilla Lua has this, that this is io.read. Here is an example:

-- Lua
local name
io.write("Hey, what is your name?\n")
name = io.read()
io.write(string.format("Hello, %s.", name))

I can’t use this since the io library is disabled in Roblox.

There are other languages that support this as well. Here are a few examples:

// C++
#include <iostream>
using std::cout;
using std::cin;
using std::string;

int main() {
    string name;
    cout << "Hey, what is your name?\n";
    cin >> name;
    cout << "Hello, " << name << '.';
}
// C
#include <stdio.h>

int main() {
	char name[20];
	printf("Hello, what is your name?\n");
	scanf("%s", name);
	printf("Hello, %s.", name);
	return 0;
}
# Python

name = raw_input("Hey, what is your name?\n");
print("Hello, %s." % name);

Is there a way in roblox of getting input like this?

2 Likes

Since Roblox is more of an interactive/object oriented environment, such method of obtaining input is not added into the engine. The only way to obtain input is by using services such as UserInputService, ContextActionService, Text Boxes etc.

The examples you have provided above are as is because there is no other way for developers to give input.

Edit:
You could create something similar if you were to print the input you wanted to give and listen for any new output messages, just filter out the unnecessary parts of the string.

8 Likes

You can use this event in LogService

2 Likes

This is fired when text is outputted, not inputted, hence the name

Studio has no interactive console, only an output window and a separate command line which has nothing equivalent to standard input available.

1 Like

I think @gillern is sorta extending this

I would not recommend this whatsoever. Use UserInputService, ContextActionService and other methods. This is a very bad way of getting user input

I was looking for that but in a way which when something is printed by a script, it could be read by another…
This topic is interesting…

I’m pretty sure input in this scenario refers not to controls but rather to what’s sent to the console, hence the content of the OP.

I think there’s a misconception running around here that the input being referenced in the OP relates to input methods rather than input as in something being entered.

MessageOut is fine for OP’s use case, at least for seeing what’s sent to the output.

1 Like

I was looking for this also. As Roblox is a game engine, the console input is dropped (for some reason). Anyway, I will use user text box to get text input.

https://developer.roblox.com/en-us/api-reference/class/TextBox

It is more complicated but I will package it in a function and use that. Let’s see how it goes.

My use case here was a code learning environment for single player. I could have used vanilla lua interpreter but then students would be confused between that environment and Studio. So, the input system needs to work inside Studio.

Local script in text box element FireOnServerRequest

-- Services
local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

-- Events
local userInputReady  = replicatedStorage:WaitForChild("UserInputReady")
local userInputRequested  = replicatedStorage:WaitForChild("UserInputRequested")

local textBox = script.Parent

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

local text = ""

-- Clear the text when user starts
local function onFocused()
	textBox.Text = ""
end

-- If focus is lost with Enter key, fire the local event
local function onFocusLost(enterPressed, inputObject)
	if (enterPressed)  then
		userInputReady:Fire(textBox.Text)
	end
end


-- When server requests input, return text
local function onUserInputRequested()
	textBox:CaptureFocus()
	local text = userInputReady.Event:Wait()
	text = textBox.Text
	return text
end
	
textBox.Focused:Connect(onFocused)
textBox.FocusLost:Connect(onFocusLost)

userInputRequested.OnClientInvoke = onUserInputRequested

Module script in ServerStorage : IO

local IO = {}

-- Services
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")

-- Event
local userInputRequested  = replicatedStorage:WaitForChild("UserInputRequested")

-- Functions
function IO.readInput(player)
	local text = ""
	text = userInputRequested:InvokeClient(player)
	print(text)
	return text
end

return IO

ServerScriptService script UserInput

local serverStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local moduleScripts = serverStorage:WaitForChild("ModuleScripts")
local IO = require(moduleScripts.IO)

-- NB: Single player only
while (table.getn(Players:GetPlayers()) == 0) do
	wait()
end
local player = Players:GetPlayers()[1]

-- The main script 
print("Enter number 1")
local number1 = IO.readInput(player)
print("Enter number 2")
local number2 = IO.readInput(player)
print("Average is", ((number1 + number2) / 2))

-- Main end

Create the needed events (1 bindable event and 1 remote function)
Create StarterGui with the TextBox and the local script under it

Note : This only works in Studio, with single-player, like described above in use case.

LogService:GetLogHistory()