How to detect [if textbox = "text" then]?

Basically, I have a part in Workspace which has a ProximityPrompt inside of it, and once the player interacts with it a GUI appears that has a TextBox.

I wanted to make it so that if the player puts in a specific Text inside the TextBox and hits enter, a sound will play + a MeshPart in ReplicatedStorage will be positioned to a specific location in the game.

The thing is, I want all of this to just be local, so that other players can’t see it in-game.

How do I do this?

1 Like
if textbox.Text == "whatever" then
-- do yes
end
3 Likes

Textbox.Text is how… you just do Textbox.Text = “string”. Oh wait I see what you mean now hold on.

textbox.FocusLost:Connect(function(enterpressed)
end)
1 Like

Apologies as I barely have any scripting knowledge.

If I put the stuff I mentioned in my post above inside the TextBox’s localscript, will that work and make it only local to each player?

And I assume inside the proximityprompt’s serverscript I just put the function to make the GUI appear to the player?

Yeah, but make sure you know which player you’re making the gui appear to on the server.

1 Like

if you edit the text content of a textbox only the client will see the changes

1 Like

First, if you may need to make the TextLabel client only. So Instance.new(“TextBox”) and parent it, whatever, then you can set up a simple detector local script:

local textBox = script.Parent

local function FocusLost()
	print("Focus Lost!")
end

textBox.FocusLost:Connect(FocusLost)

Edit: I forgot if you need this to be client sided (instance.new) or if you can just use it from the server. I think you can though.

Edit2: You don’t need onFocused, since we only care about the text after it has been updated, FocusLost is better.

1 Like

Local scripts are kinda in the name, they’re like your local neighborhood you know? Basically, only the player who is running the local script will see the changes.

1 Like

That’s good, but I think you forgot one of the parameters. The enterpressed boolean will check whether or not the focus was lost by pressing enter, which is what I think OP wants:

1 Like

Yeah, then the OP can just do

if textBox.Text == "String" then
    print("It's correct!")
end
1 Like

Yeah but obviously within the event connected to the function (with the parameters), I’m sorry if I sound stupid a bit my english isn’t very fluent.

Full script:

local textBox = script.Parent -- Set up textbox variable here

local function FocusLost()
	print("The player has lost focus of the TextBox, check the variable now.")
    if textBox.Text == "String" then
        print("It's correct!")
    -- Run extra code here if needed
    end
end

textBox.FocusLost:Connect(FocusLost)

You know what, it’s fine… maybe I just can’t get my point across that well.

Oh you mean like activating it in one line? You don’t really need it but sure, we can put it in.

Full script:

local textBox = script.Parent -- Set up textbox variable here

local key = "String" -- Set this to be what string you want to check

textBox.FocusLost:Connect(function(inputThatCausedFocusLoss)
	print("The player has lost focus of the TextBox, check the variable now.")
    if textBox.Text == key then
        print("It's correct!")
        -- Run extra code here if needed
    end
end)
1 Like

I assume this must be correct?

LocalScript inside TextBox: (Sound is also inside TextBox so that it plays locally)

local textBox = script.Parent
local sound = script.Parent.Sound

local rs = game:GetService("ReplicatedStorage")
local model = rs:WaitForChild("OrangeCoin") 
local pos = game.Workspace.MusicPartTP.CFrame

textBox.FocusLost:Connect(function(enterpressed)
	
	if textBox.Text == "CODE" then
		print("It's correct!")
		sound:Play()
		model.Parent = workspace
		model:SetPrimaryPartCFrame(pos.CFrame)
	end
	
end)

ServerScript inside ProximityPrompt: (SongGUI is also inside ProximityPrompt)

local prompt = script.Parent

prompt.Triggered:Connect(function(player)
	if player then 
		local SongGui = player:WaitForChild("PlayerGui"):WaitForChild("SongGUI")
		SongGui.Enabled = true
	end
end)

Close, with the enterpressed parameter on the focuslost event, you aren’t actually doing anything with it, the whole reason of that parameter is to check if the focus was lost by someone clicking the enter key so just add this line of code:

textBox.FocusLost:Connect(function(enterpressed)
if enterpressed == true and textBox.Text == "CODE" then
		print("It's correct!")
		sound:Play()
		model.Parent = workspace
		model:SetPrimaryPartCFrame(pos.CFrame)
end
end)
1 Like

For some reason the GUI doesn’t appear when I click the proximityprompt, did I do a mistake in the serverscript inside the proximityprompt? or is it because the GUI is inside the proximityprompt?

This appears in output:

Can you send an image of your entire GUI explorer? I think you may have put “GUI” instead of “Gui”?

I have created what you mentioned. You will need to tweak some things.

local proximityPrompt = workspace.Part.ProximityPrompt -- path to proximityprompt

local replicatedStorage = game:GetService("ReplicatedStorage")

local textBox = script.Parent.TextBox -- path to textbox

local meshpart = replicatedStorage:WaitForChild("MeshPart") -- path to meshpart
local sound = replicatedStorage:WaitForChild("Sound") -- path to sound

proximityPrompt.Triggered:Connect(function()
	textBox.Visible = true
end)

textBox.FocusLost:Connect(function(enterPressed)
	if enterPressed and textBox.Text == "meow" then -- put specific text here
		meshpart.Parent = workspace
		meshpart.Position = Vector3.new(0, 0.5, 0) -- put position here
		sound:Play() -- play sound
	end
end)

Here is a file including the proximity prompt, gui, sound, and mesh part: meow.rbxl (53.7 KB)

That means for some reason songGUI doesn’t exist, did you put the songgui in startergui so that it would replicate to each player’s playergui? Or you might’ve misspelled?