How do I make a custom chat?

I am making a custom chat and I know how to do everything except for get the event when the player presses buttons. So I have a variable called chatting and if that is true then I want to get the players keyboard input. Obviously I can use keydown but that doesnt get capitalized letters or any of these: -;'± etc.

Basically I just want an event for when a player presses a key and it gives me the key that they pressed.If theres no event then I need a way to figure the key out because even with string.byte(key) it doesn’t get the capitalized letter.

7 Likes

The KeyPressed stuff I believe is deprecated. You should be using the UserInputService.

1 Like

Use the UserInputService to detect an input when a key is pressed.

Example:

local UIS = game:GetService("UserInputService");

UIS.InputBegan:Connect(function(input)
    -- Change "F" to whatever your input is.
    if input.KeyCode == Enum.KeyCode.F then
        -- Do stuff.
    end
end)

Simple as that. For a chat script, make sure to account for when the player’s .Chatted event fires OR use a completely custom system and detect “slash” key inputs or “escape”/“return” inputs.

4 Likes

Just tried it. How would I get a lowercase f though because you can’t do Enum.KeyCode.f

2 Likes

UserInputService supports both lowercase and uppercase lettering. If you need only lowercase lettering, map out your inputs in a table and match it to a string using string.lower();

Try the following:

local test = {
    [Enum.KeyCode.F] = "f"
};

local returnVariable = [input];

EDIT: To detect if the key itself is lowercased, add a “leftshift”, “rightshift”, or “capslock” detection to see if the key is uppercased. I believe, since the key is an Enum, you can’t use tostring(). However, I’ve seen people use key bytes (I think it’s key bytes-basically the numbers equivalent to the key).

8 Likes

Make sure to filter it on the server side and with remoteevent Fire the filtered text to all clients

Event:FireAllClients(Sender,Message)

Also, when you are filtering the text make sure to use pcall() it is connected to http so it would sometimes throw error also it would be good if you add something like this in client side

if (string.len(TextBox.Text) > 2) then
   Event:FireServer(TextBox.Text)
end

This checks if textbox is empty or not or if player typed more then 2 letters in it.

2 Likes

Does not matter if it is upper or lower it will still continue I believe.

or just do

local UIS = game:GetService("UserInputService");

UIS.InputBegan:Connect(function(input)
    -- Change "F" to whatever your input is.
    if string.upper(input.KeyCode) == Enum.KeyCode.F then
        -- Do stuff.
    end
end)
4 Likes

I know it will continue but I want to be able to know the difference. I want to use the KeyCode and see if the key is upper or lower case. I dont want the person to only be able to type capitals or non caps. Also I put that code in and string.upper errors

1 Like

And the error is?

2 Likes

Thank you for the edit I believe you solved my problem

2 Likes

The error is that the parameter is a EnumItem not a string

1 Like

Ohhh, yeah

input.KeyCode 

is same as

Enum.KeyCode.F -- for example
2 Likes

To convert input.KeyCode to a string I believe that you can do input.KeyCode.Name

4 Likes

Thank you. I am happy I didn’t start last night because your post would of saved me a lot of code

2 Likes

Wouldn’t recommend doing that as players could then type vertically. That’s why the built in Roblox chat censors 1 (and possibly 2, I don’t remember) letter chats.

1 Like

Hmm, I have to agree with you.

But it’s a developer’s choice.

1 Like

Cant you just use a text box? You would just check the text in it.

Tbh I never thought of textbox so I spent like 4 hours making custom chat and now I realize I can make it better and I could’ve done it in like 20 minutes. Thanks

2 Likes