TextBox Focus Issue

I am trying to make a KeyboardInput utility that will return the string inputted. But My issue is if you make a typo and press no (if Dialogue:PromptYesOrNo(IsCorrectPrompt) then), then the script will continue instead of yielding until if Dialogue:PromptYesOrNo(IsCorrectPrompt) then returns true. Help?

function Dialogue:KeyboardInput(Message, Placeholder, Finished)
	if Dialogue.Enabled == false then
		Dialogue.Enabled = true
		Dialogue.BroadcastBox.Position = UDim2.new(0.0, 0.0, 1.0, 0.0)
		Dialogue.BroadcastLabel.Text = ""
		TweenBroadcastBoxIn:Play()
		task.wait(1.0)
	end
	
	TypeWriteText({
		Object = Dialogue.BroadcastLabel,
		Text = Message,
		SoundEnabled = true,
		Time = Dialogue.Speed})
	
	local InputBox = CreateObject "TextBox" {
		BackgroundColor3 = Color3.new(0.0, 0.0, 0.0),
		BorderSizePixel = 0.0,
		BackgroundTransparency = 1.0,
		ClearTextOnFocus = true,
		Text = Placeholder,
		TextColor3 = Color3.new(1.0, 1.0, 1.0),
		TextScaled = true,
		TextWrapped = true,
		Font = Enum.Font.PatrickHand,
		PlaceholderText = Placeholder,
		Name = "InputBox",
		Position = UDim2.new(0.25, 0.0, -1.0, 0.0),
		Size = UDim2.new(0.5, 0.0, 0.3, 0.0),
		ZIndex = 1.0, Parent = Gui,
		
		CreateObject "UICorner" {
			CornerRadius = UDim.new(0.1, 0.0)
		},
		
		CreateObject "UIStroke" {
			Thickness = 2,
			ApplyStrokeMode = "Border"
		},
		
		FocusLost = function(EnterPressed)
			if EnterPressed then
				if string.len(Gui:WaitForChild("InputBox").Text) >= 10 then
					Dialogue.BroadcastLabel.Text = "Name too long!"
				else
					Blip:Play()
					
					TweenService:Create(Gui:WaitForChild("InputBox"),
						TweenInfo.new(2.0,
							Enum.EasingStyle.Quart,
							Enum.EasingDirection.Out
						),
						{
							Position = UDim2.new(0.25, 0.0, -1.0, 0.0),
							BackgroundTransparency = 1
						}
					):Play()
					
					local IsCorrectPrompt = (Gui:WaitForChild("InputBox").Text..", is that correct?")
					if Dialogue:PromptYesOrNo(IsCorrectPrompt) then
						Signal:fire(Gui:WaitForChild("InputBox").Text)
						Gui:WaitForChild("InputBox"):Remove()
						if Finished then
							Dialogue.Enabled = false
							TweenBroadcastBoxOut:Play()
							task.wait(1.0)
						end
					else
						TweenService:Create(Gui:WaitForChild("InputBox"),
							TweenInfo.new(2.0,
								Enum.EasingStyle.Quart,
								Enum.EasingDirection.Out
							),
							{
								Position = UDim2.new(0.25, 0.0, 0.2, 0.0),
								BackgroundTransparency = 0.5
							}
						):Play()
						Gui:WaitForChild("InputBox"):CaptureFocus()
					end
				end
			end
		end
	}	
	
	TweenService:Create(InputBox,
		TweenInfo.new(2.0,
			Enum.EasingStyle.Quart,
			Enum.EasingDirection.Out
		),
		{
			Position = UDim2.new(0.25, 0.0, 0.2, 0.0),
			BackgroundTransparency = 0.5
		}
	):Play()

	local StringInput = Signal:wait()
	return StringInput
end

To make your KeyboardInput utility yield until the user enters the correct input, you can replace the line where you’re waiting for the user to confirm if the input is correct using the PromptYesOrNo method with a loop that waits for the user to confirm.

local function WaitForInputConfirmation(prompt)
	local confirmed = false
	while not confirmed do
		if Dialogue:PromptYesOrNo(prompt) then
			confirmed = true
		end
		task.wait()
	end
end

function Dialogue:KeyboardInput(Message, Placeholder, Finished)
	-- your existing code here

	local IsCorrectPrompt = (Gui:WaitForChild("InputBox").Text..", is that correct?")
	WaitForInputConfirmation(IsCorrectPrompt)

	-- continue with the rest of your code as before
end

This code defines a new function called WaitForInputConfirmation , which takes a prompt as its argument and waits until the user confirms it using the PromptYesOrNo method. It does this by entering a loop that checks for the user’s confirmation and waits one frame before checking again.

Just as a code review, you should add some error handling as well!

1 Like

Hmm, even when the PromptYesOrNo utility returns false, it still does not yield. Do you have any other ideas on why this is happening?

Oh, could you please give me a few examples on some?

Never mind! I figured this one out by rearranging some code.

Sure thing, uses of error, pcall and asset to check if conditions are met etc.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.