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 :
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.
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?
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.
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.
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 !
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"
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