Preventing string.sub from cutting off,removing and duplicating words

Hello, I have a problem with strings.

I made my own chat in which everything works fine except this part: I made a script that only let’s 25 characters on each line so if you type something that is 25+ characters it will be split, just so it prevents the text from getting very small.

Example

“BawTheSeal: Less than 25 chars” -first message
“BawTheSeal: More than 25 (continuous a’s)” -second message
Screenshot_1040

The problem is that some words get cut off or removed, example:

String = “Hello, my name is Baw and I’m here to tell you that you are amazing.”

Chat:
Screenshot_1041

As you can see it has cut off and removed some words (name,is,baw,and) and if you look at the word “that” it duplicated the a.

Script:

local ChatStorage = game.Workspace.ChatStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("FiredChat")
local remoteEventTxt2Long = ReplicatedStorage:WaitForChild("MsgTooLong")


remoteEvent.OnServerEvent:Connect(function(plr,msg)
local user = plr.Name..": "

if msg:len() >= 25 then
local stringval = Instance.new("StringValue",ChatStorage)
stringval.Name = user..string.sub(msg,1,25 - user:len())

local stringval2 = Instance.new("StringValue",ChatStorage)
stringval2.Name = string.sub(msg,25,50)

if stringval2.Name:len() >= 25 then
local stringval3 = Instance.new("StringValue",ChatStorage)
stringval3.Name = string.sub(msg,50,75)

if stringval3.Name:len() >= 25 then
remoteEventTxt2Long:FireClient(plr)

end
end
else
local stringval = Instance.new("StringValue",ChatStorage)

stringval.Name = plr.Name..": "..msg
end
end)

Is there a way to accomplish it not cutting off,removing or duplicating words?

Note: I am very new to strings so, sorry if the answer is a really easy thing, string.sub is the only function i know that removes characters.

I have a question, say for example at the 25 character limit per line say there is a word that is not completed and you cut it off, how would like to handle that by using a dash on a new line or by just completing the word and then do a new line.

1 Like

Remove the remaining characters of the word and then a complete it in a new line.

Example:
“Hello my name is Ba- (The word that gets cut off)
w and I’m here to…”

To this:

“Hello my name is
Baw, and I’m here to…”

Hope this is clear!

1 Like

I think this is a roblox thing, some bugs with strings. If you are making a command script you will see that a space is adding at the end of your string and you can chat empty chats.
Also to make a custom chat systeme you need to filter your messages
Heres how to make filter chat:

1 Like

Oh, thank you for the information! I appreciate it, if it is a Roblox problem i’ll have to look for an alternative solution in the meantime. Oh and I already have a filter chat that works! Thank you for your concerns.

This will make your current solution work but not give you the effect of moving the word to a new line, you need to start each sub-string off at one number ahead of the previous one like this:

local ChatStorage = game.Workspace.ChatStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("FiredChat")
local remoteEventTxt2Long = ReplicatedStorage:WaitForChild("MsgTooLong")


remoteEvent.OnServerEvent:Connect(function(plr,msg)
    local user = plr.Name..": "
    
    if msg:len() >= 25 then
        local stringval = Instance.new("StringValue",ChatStorage)
        stringval.Name = user..string.sub(msg,1,25 - user:len())
        
        local stringval2 = Instance.new("StringValue",ChatStorage)
        stringval2.Name = string.sub(msg,25 - user:len() + 1,50)
    
        if stringval2.Name:len() >= 25 then
            local stringval3 = Instance.new("StringValue",ChatStorage)
            stringval3.Name = string.sub(msg,51,75)
            
            if stringval3.Name:len() >= 25 then
                remoteEventTxt2Long:FireClient(plr)
            end
        end
    else
        local stringval = Instance.new("StringValue",ChatStorage)
        stringval.Name = plr.Name..": "..msg
    end
end)
1 Like

Thanks for your time! It works now.

1 Like