Help with TextBox

I’m making a vending machine like thing, and I want it so that if a player types a certain word, they get that drink. I have already attempted at working this out, here’s my script

-- script in textbox
local textBox = script.Parent
local function onFocusLost(enterPressed, inputObject)
	if enterPressed then
		local coffee = textBox.Text
		if coffee == "Coffee" then
            print("yay")
		else
            print("boo")
		end
	end
end
 
textBox.FocusLost:Connect(onFocusLost)

When you say

   local coffee = textbox.Text

Things might get messed up if you contain a property in a variable. Try saying:

   local coffee = textbox
   if coffee.Text == “Coffee” then
1 Like

the coffee variable isnt necessary then, so just use textbox

1 Like

On this part

local coffee = textBox.Text

Try making the “t” in TextBox uppercase.

In the future I would refrain from containing properties in variables. You should call a property from outside of the variable to make sure you aren’t trying to reference an object.

1 Like

Is this a server or local script? If it’s a server script, the server can’t see what the player has entered. I seem to have it working, but mine is in a local script.

I tried this:

local TextBox = script.Parent
local Text = TextBox.Text
local function onFocusLost(enterPressed, inputObject)
	if enterPressed then
         if Text == "Coffee" then 
            print("yay")
		else
            print("boo")
		end
	end
end
 
TextBox.FocusLost:Connect(onFocusLost)

That would not work because that is a constant text. You can keep it inside of the function. If this is a server script it won’t work. It needs to be local.

2 Likes

Ok it prints, but when I typed in “Coffee” it printed “Boo”

1 Like

I’m assuming this is a server script, but please confirm if it is or not. The reason it says boo is because the server sees nothing when you press enter, because typing in a text box is a client change. You just need to simply change it from a server to local script.

I did though…

local TextBox = script.Parent
local function onFocusLost(enterPressed, inputObject)
	if enterPressed then
         if TextBox.Text == "Coffee" then 
            print("yay")
		else
            print("boo")
		end
	end
end
 
TextBox.FocusLost:Connect(onFocusLost)

Local Script inside of the textbox. The server can’t read what the player entered
here’s your script when using all of the other solutions posted here.

2 Likes

You did change it to local…? Or you did confirm, because I don’t see clarification from you anywhere about the type of script

Please take @bossay6‘s script because it should work perfectly for what you need.

Make sure this goes in a localscript.

Is there a way to get the player by typing in the textbox?

local player = game.Players.LocalPlayer. Since it has to be a local script, that’s the only person who could be typing in it. Unless you are trying to do something where everyone sees someone typing, but that would require remote events and all.

Yeah I want everyone to see that person typing, and also to give that person something