Storing TextBox input

  1. What do you want to achieve? A system which sets a variable for what the player types in a textbox

  2. What is the problem? I don’t know what line of code to use

  3. What solutions have you tried so far? local TextBoxInput = TextBox.Text

6 Likes

You’re going to need to provide more information than you have. Your current solution correctly solves the problem you have presented.

Perhaps you want to keep the value you store in the variable equal to the text the player has entered, in which case you can do the following:

local textBoxInput = TextBox.Text

TextBox:GetPropertyChangedSignal("Text"):Connect(function ()
    textBoxInput = TextBox.Text
end)

Or, perhaps you want to update the value when the user loses focus on the textbox:

local textBoxInput = ""

TextBox.FocusLost:Connect(function (enterPressed)
    if not enterPressed then
        return
    end
    textBoxInput = TextBox.Text
end)
10 Likes

Unless you’ve got datastores, to temporarily store player input, for each player you would need to create a key (his name) and his input (the text) in a table.
To refer to the text he types you would also set a variable .
(Your title says “storing input” so i’ll give you a solution for that as well as referencing the text) .
You would create a dictionary and insert key and value pairs for each input.
I created a script in which measures are taken to stop duplicate entries, and to store input for players in a table on the Server.

A local script in the Text Box :

  --// Dependencies/variables
  local ReplicatedStorage = game:GetService("ReplicatedStorage")
  local event = ReplicatedStorage:FindFirstChild("Event")
  local box = script.Parent
  --//function

  box.FocusLost:Connect(function()--when he has finished entering text, do this
    local text = box.Text--you reference the text like this
   event:FireServer(tostring(text))--we send the text to the server
  end)  

Next we ‘grab’ that data through the Server and store the data for the player.

Script in ServerScriptService :

  --// Table

 input_storage = {
	
   Username = "text";
	
	}

  --// Dependencies
  local ReplicatedStorage = game:GetService("ReplicatedStorage")
  local event = ReplicatedStorage:WaitForChild("Event")
  
--//function
  
 event.OnServerEvent:Connect(function(player,text)--for the player and his text
	 local name = tostring(player.Name)
	     local enteredvalue 
	if type(text)~= "string" then  local text = tostring(text)
		   print("not a string")
		return text
	end
    
       if input_storage[name] then input_storage[name] = nil -- if the text already exists then prevent same text being inserted
	 else
	    input_storage[name] = text
		   for key,value in pairs(input_storage) do
	    print(key .." has entered this text : "..value.."!")
          end
        end
     end)

After the event is fired from a local script in the text box, the text is sent to the server which creates a key,value pair in a dictionary for each player and his text that he inputted. Duplicate entries from the same client won’t be accepted , but if it’s new then the previous entry (pair) will be deleted and a new one will be inserted. (You can modify it a bit if you don’t need to delete duplicate entries)

In my Local Script , the variable text in the anonymous function was used to reference / refer to the entered text after the user finished data entry.

3 Likes

It’s much simpler than all the complicated solutions.

Simply get a remote event and fire it to get the text:

local RemoteEvent = game:GetService("ReplicatedStorage").EventName
local TextBox = script.Parent

TextBox.FocusLost:Connect(function()
    RemoteEvent:FireServer(TextBox.Text)

New script:

 local RemoteEvent = game:GetService("ReplicatedStorage").EventName
 RemoteEvent.OnServerEvent:Connect(function(player, text)
     local TextBoxVariable = text

And your good to go!

None of these solutions are complicated. They do exactly what you’re doing with a few more lines involved for extended (and probably necessary) functionality.

woot3’s examples are two: tracking the input of a TextBox as a player types and tracking it as in when the player explicitly presses enter rather than sending any input regardless if the player loses focus of the TextBox.

ElectroFusion’s involves having the server track the value in case it ever needs it, among guarding against invalid input sent by the client (though its not fully done correctly, has a couple of returns where they don’t belong and all).

2 Likes

I know this was long, long ago, but do you mind telling me how to get the .EventName?

I would appreciate it greatly.

Are you talking about the Name property?

Just use Event.Name, the example provided used an event called “EventName” for exemplar purposes solely, you can stay calm and call your remotes whatever you want.

I also realize my coding style from 8 months ago is horrendous.

3 Likes