Textbox:CaptureFocus() creating a space after the text when called

Hello everyone !

I’m currently trying to make my own chat with admin command included. As i’m using the enter key to open the chat, i need to use :CaptureFocus(). The problem is :CaptureFocus() seems to always add a space to the text, even if i said nothing. I even tried to do TextBox.Text = “” after the :CaptureFocus() but it doesn’t seem to work. I would love if someone could tell me how i could remove this space at the start. So when i do :CaptureFocus(), it’d be just “” and not " ".

Why do i need this ? Well to detect when a player is typing a command, i’m doing :

if Text:sub(1, 1) == "/" then

But because the CaptureFocus() fct add a " " at the start, it will not detect it :

ccc33739ca6bb6254c7b1ef50e9d93d3 1dc247e2b4bffea11d898cb1fd05d578

Is there any other way around it ? I also need to delete this " " because i’m spliting the string with " " later on, and it’ll cause some problems if it’s not removed.

2 Likes

Huh, that’s really strange. I haven’t seen that problem before when I use CaptureFocus. Is ClearTextOnFocus disabled by accident? Is there some sort of script which handles a .Focused event and for some reason would add a space to the text?

1 Like

ClearTextOnFocus is disabled, because I don’t want to delete the message of the player if he clicks away from the textbox. If it’s enabled, will it not put a space at the start ? if yes, then i think i have a solution.

Not really :confused:

Here is my Textbox :

Is the “Text” property accidentally " " instead of “” ?

No and if i already have some text, and do Capturefocus() it’ll add a space after the text

That’s super odd, not sure what could cause that. I wish I had a definitive answer but I guess I’m just not sure at this point.

1 Like

Me neither :sweat_smile: 30char

So i decided to investigate a little bit more with this code :

local LastMessageWritten = ""
self.ChatBar:CaptureFocus()
print("w"..LastMessageWritten.."t"..LastMessageWritten.."f")
self.ChatBar.Text = LastMessageWritten
print("w"..self.ChatBar.Text.."t"..self.ChatBar.Text.."f")
wait(5)
print("w"..self.ChatBar.Text.."t"..self.ChatBar.Text.."f")

This is the output :

1f90e17ab702963d4af196b185e57edd

As you can see, the first print after doing CaptureFocus() is correct, but meanwhile those 5 seconds, a space is added to the text, even if i didn’t touch my keyboard, and the last print proves it.

Enabling ClearTextOnFocus didn’t solve the issue, neither changing all the parameters of the textbox.

So i guess this is a bug ?

Weird…

1 Like

So, after cursing a lot at this TextBox, i found something we could call a solution :

Since CaptureFocus is adding a space at the beginning and i don’t want to do smthing like :

self.ChatBar:CaptureFocus()
wait()
self.ChatBar.Text = LastMessageWritten

because there is a small delay between those 2 and it’d look ugly, let’s just not delete this space ! I have an event when the text is changed, so if the player tries to set the text to “” just say no and put back the cursor in place :

if self.ChatBar.Text == "" then
    self.ChatBar.Text = " "
    self.ChatBar.CursorPosition = 2
end

This look 10 times better. Hope this thread will be able to help anyone experiencing this ! :smiley:

1 Like

I stumbled upon this problem today, solved with this:

local function IsEmpty(str)
	return str:match("^%s*$") ~= nil
end

local function ereaseFrontSpaces(msg)
	if not IsEmpty(msg) and IsEmpty(string.sub(msg,1,1)) then
		msg = string.sub(msg,2,-1)
		msg = ereaseFrontSpaces(msg)
		return msg
	else
		return msg
	end
end

msg = ereaseFrontSpaces(msg)

-- msg = "         hi whatsup"
-- newmsg = "hi whatsup"
1 Like

For anyone stumbling upon this thread, don’t follow any of the coding advice above. If Roblox ever comes along and fixes this bug, then all of these examples will break. Here is a non-invasive solution:

game:GetService("RunService").RenderStepped:Wait() --fixes the bug (must be executed on the line before the CaptureFocus() )
textBox:CaptureFocus() --now you can call the CaptureFocus() function
7 Likes

Just add a “task.wait()” before capturing focus.

1 Like