Making a custom chat but / comes in the way

Hello, I am making a custom chat, everything works fine, but there’s a problem, whenever I press / key, the textbox gets focused but / gets put into the textbox, and it’s annoying.
ScreenShot:

Any help please

Basically, remove the first character (which is /).

local text = string.sub(message, 1)

textBox.Text = text
1 Like

I have tried using that multiple times before, but it doesn’t work.

Hmm, ok.

What is your code? It’s much more difficult to assist you if we can’t see how your code works.

UserInputService.InputBegan:Connect(function(input, gp)
    print("Began")
    if input.KeyCode == Enum.KeyCode.KeypadDivide and gp == false then
        print("Pressed")
        Messagebox:CaptureFocus()
        local text = string.sub(Messagebox.Text, 1, #Messagebox.Text - 1) -- Ignore this line, it doesn't work.
        Messagebox.Text = text
    elseif input.KeyCode == Enum.KeyCode.Return then
        if Messagebox.Text ~= "" then
            print("Message detected. Sending to server")
            SendMessageToServer:FireServer(Messagebox.Text)
        end
    end
end)

Nevermind, I just put a bit of delay like wait(0.04), it works now.

Which part of your code records keys being pressed (i.e a, b, c as if I were typing into a chat bar)?

Do this:

UserInputService.InputBegan:Connect(function(input, gp)
    print("Began!")
    if input.KeyCode == Enum.KeyCode.KeypadDivide or input.KeyCode == Enum.KeyCode.Slash and gp... oops

I feel like I just wasted time, but glad you found the answer.

However, you really may want to consider putting the Slash as an option, because for example I never use the Divide key on the Keypad for opening chat.

Their using a TextBox which handles text input on it’s own. The script is just there to focus the textbox when the player presses /.

I always use the Divide key for opening the chat

Okay, but what I’m saying is not many people do at all. I’d still recommend using an or statement and putting both.

1 Like

I put an or statement right after you suggested.

I do not recommend any of the above recommendations, and instead, you should use the “RunService”, and specifically:

game:GetService(“RunService”).RenderStepped:Wait()

I don’t see any use of RunService in this case.

All you need to do, is add game:GetService(“RunService”).RenderStepped:Wait() above Messagebox:CaptureFocus()

Hope this helps. :slight_smile:

2 Likes

More information here: CaptureFocus in InputBegan handler is causing the TextBox.Text to also equal the key that was pressed on

omg you are a legend ily <3 you are so so so amazing. this is awesome :smiley:

1 Like