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)
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.
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.
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.
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.
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.
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.