You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
I want to change a piece of text that has been selected.
-
What is the issue? Include screenshots / videos if possible!
I don’t know how to change the text IN the selection.
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have already looked on the DevForum and on the Developer Hub. I edited the script below, but now I’m a bit stuck.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
This is my script:
local textBox = script.Parent.Parent.Parent.TextBox
local selectedText = {}
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
else
selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
end
end
script.Parent.MouseButton1Click:Connect(function()
textBox.Text = textBox.Text.."<b>"..selectedText.."</b>"
end)
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)
Thanks
1 Like
It works well, for me. What do you want exactly?
I should have explained it more clearly, here is an illustration:
Ok so, try to use this script:
local textBox = script.Parent.TextBox
local selectedText = {textBox.Text, "", ""}
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
selectedText = {textBox.Text, "", ""}
else
selectedText = {
[1] = string.sub(
textBox.Text,
0,
math.min(textBox.CursorPosition, textBox.SelectionStart) - 1
),
[2] = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart) - 1
),
[3] = string.sub(
textBox.Text,
math.max(textBox.CursorPosition, textBox.SelectionStart),
string.len(textBox.Text) + 1
)
}
end
end
script.Parent.TextButton.MouseButton1Click:Connect(function()
textBox.Text = selectedText[1] .. "<b>" .. selectedText[2] .. "</b>" .. selectedText[3]
end)
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)
1 Like
Sorry for the late reply, but this script seems to put the new text at the back
Ok.
local textBox = script.Parent.Parent.Parent.TextBox
local textButton = script.Parent
local prevSelectedText = {textBox.Text, "", ""}
local selectedText = {textBox.Text, "", ""}
local timeElapsed = os.clock()
local function showSelection()
local verifyNewText = selectedText
timeElapsed = os.clock()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
selectedText = {textBox.Text, "", ""}
else
selectedText = {
[1] = string.sub(
textBox.Text,
0,
math.min(textBox.CursorPosition, textBox.SelectionStart) - 1
),
[2] = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart) - 1
),
[3] = string.sub(
textBox.Text,
math.max(textBox.CursorPosition, textBox.SelectionStart),
string.len(textBox.Text) + 1
)
}
end
if verifyNewText[1] ~= selectedText[1] or verifyNewText[2] ~= selectedText[2] or verifyNewText[3] ~= selectedText[3] then
prevSelectedText = verifyNewText
end
end
textButton.MouseButton1Click:Connect(function()
if os.clock() - timeElapsed < 0.12 then
textBox.Text = prevSelectedText[1] .. "<b>" .. prevSelectedText[2] .. "</b>" .. prevSelectedText[3]
else
textBox.Text = selectedText[1] .. "<b>" .. selectedText[2] .. "</b>" .. selectedText[3]
end
prevSelectedText = {textBox.Text, "", ""}
selectedText = {textBox.Text, "", ""}
end)
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)
2 Likes
Thanks! This script works well except for that it only works once, but i removed one line of code and now it works perfectly.
local textBox = script.Parent.Parent.Parent.TextBox
local textButton = script.Parent
local prevSelectedText = {textBox.Text, "", ""}
local selectedText = {textBox.Text, "", ""}
local timeElapsed = os.clock()
local function showSelection()
local verifyNewText = selectedText
timeElapsed = os.clock()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
-- I removed this
else
selectedText = {
[1] = string.sub(
textBox.Text,
0,
math.min(textBox.CursorPosition, textBox.SelectionStart) - 1
),
[2] = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart) - 1
),
[3] = string.sub(
textBox.Text,
math.max(textBox.CursorPosition, textBox.SelectionStart),
string.len(textBox.Text) + 1
)
}
end
if verifyNewText[1] ~= selectedText[1] or verifyNewText[2] ~= selectedText[2] or verifyNewText[3] ~= selectedText[3] then
prevSelectedText = verifyNewText
end
end
textButton.MouseButton1Click:Connect(function()
if os.clock() - timeElapsed < 0.12 then
textBox.Text = prevSelectedText[1] .. "<b>" .. prevSelectedText[2] .. "</b>" .. prevSelectedText[3]
else
textBox.Text = selectedText[1] .. "<b>" .. selectedText[2] .. "</b>" .. selectedText[3]
end
prevSelectedText = {textBox.Text, "", ""}
selectedText = {textBox.Text, "", ""}
end)
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)
No, I tried and it works any time. Are you sure it only works once?
Hmmm, for me it only worked once and then it would malfunction by placing the text at the back again.