Wrapping Selected Textbox Text

  1. What do you want to achieve? Keep it simple and clear!
    A textbox that when you select certain text it ONLY replaces that text with the same text but wrapped with letter decorations I.E. ~~~ Words ~~~

  2. What is the issue?
    Tried many solutions but can’t find the right one. I have been stuck and really need help with my text wrapping project.

  3. What solutions have you tried so far? Searched through the entire Developer Forum and more but found nothing that suit my needs. I couldnt find anything that worked and nothing that did work really suit the case.

I have tried stuff such as:

lastSelection =  string.sub(textBox.Text, textBox.SelectionStart, textBox.CursorPosition)
lastSelection = lastSelection:sub(1, #lastSelection - 1)
textBox.Text = `~~~ `..lastSelection..` ~~~`

But it replaces everything in the Textbox instead of just the Highlighted/Selected text. (I can’t figure that one out either but that isnt the priority) I really need a solution to this. Any help is appreciated thanks :smiley:

(If this is wrong category or something like that please tell me and I will fix it right away)

Something like this may work

local Text = ""
Text ..= string.sub(textBox.Text, 1, textBox.SelectionStart-1)
Text ..= "%s" -- For formatting purposes later on!
Text ..= string.sub(textBox.Text, textbox.CursorPosition + 1)
lastSelection =  string.sub(textBox.Text, textBox.SelectionStart, textBox.CursorPosition)
lastSelection = lastSelection:sub(1, #lastSelection - 1)
textBox.Text = string.format(Text, "~~~ " .. lastSelection .. " ~~~"

If you need an explanation of anything that is happening in the code, let me know!
(And if it errors, let me know the error and what line it breaks on, and I’ll try to resolve it)

2 Likes

Thanks for the reply. It does work however if I put in Orange as an input and then make a button click activate this code it prints the following.

orang~~~  ~~~orange

Instead of the expected

~~~orange~~~

I attempted to debug the code but since it is a UI localscript no error output is provided.

What section did you have highlighted?

I highlighted the words orange and clicked the button. I tried reading the documentation for the functions but nothing helps

and clicked the button

That’s the issue. Clicking the button unhighlighted the text, causing it to break

Oh thanks how would I implement this system then???