I’m not exactly good with this, but I will try to help here. I believe there is a CursorPosition property of a text box, so It may go something like this:
local adds = {
['"'] = '"',
["("] = ")",
["{"] = "}",
["["] = "]",
["<"] = ">"
}
local textbox = --textbox
local prevtext = ""
local conn
function createconnection()
conn = textbox:GetPropertyChangedSignal("Text"):Connect(function()
conn:Disconnect()
local pos = textbox.CursorPosition
local typed = string.sub(textbox.Text, pos-1, pos)
if adds[typed] then
local newtext = string.sub(textbox.Text, 1, pos)..adds[typed]
if pos < string.len(textbox.Text) then
newtext ..= string.sub(textbox.Text, pos+1, textbox.Text)
end
textbox.Text = newtext
end
prevtext = textbox.Text
textbox.CursorPosition = pos
createconnection()
end)
end
createconnection()
Now, keep in mind that I haven’t tested this myself, so be prepared for some mistakes.
Okay, so I did just test this myself. It worked quite well and a few errors that needed fixing (like typing inside of a pair of quotation marks or with any character after where you were typing didn’t work, the positioning was off after fixing that, etc.) But, I have fixed it.
local adds = {
['"'] = '"',
["("] = ")",
["{"] = "}",
["["] = "]",
["<"] = ">"
}
local textbox = script.Parent.TextBox
local prevtext = ""
local conn
function createconnection()
conn = textbox:GetPropertyChangedSignal("Text"):Connect(function()
conn:Disconnect()
local pos = textbox.CursorPosition
if string.len(textbox.Text) > string.len(prevtext) then
local typed = string.sub(textbox.Text, pos-1, pos-1)
if adds[typed] then
local newtext = string.sub(textbox.Text, 1, pos-1)..adds[typed]
if pos < string.len(textbox.Text) then
newtext ..= string.sub(textbox.Text, pos, textbox.Text)
end
textbox.Text = newtext
end
end
prevtext = textbox.Text
textbox.CursorPosition = pos
createconnection()
end)
end
createconnection()
Alright, I fixed those errors you mentioned (one missing a =, another forgetting a string.len(), and another not checking for deleting letters.)
local adds = {
['"'] = '"',
["("] = ")",
["{"] = "}",
["["] = "]",
["<"] = ">"
}
local textbox = script.Parent.TextBox
local prevtext = ""
local conn
function createconnection()
conn = textbox:GetPropertyChangedSignal("Text"):Connect(function()
conn:Disconnect()
local pos = textbox.CursorPosition
if string.len(textbox.Text) > string.len(prevtext) then
local typed = string.sub(textbox.Text, pos-1, pos-1)
if adds[typed] then
local newtext = string.sub(textbox.Text, 1, pos-1)..adds[typed]
if pos <= string.len(textbox.Text) then
newtext ..= string.sub(textbox.Text, pos, string.len(textbox.Text))
end
textbox.Text = newtext
end
end
prevtext = textbox.Text
textbox.CursorPosition = pos
createconnection()
end)
end
createconnection()
Actually i just found a different error, when I type something in between the () like (t) and then delete the t instead or returning to () it instead returns ())))
It doesn’t do that for me. Here is the exact script I have:
local adds = {
['"'] = '"',
["("] = ")",
["{"] = "}",
["["] = "]",
["<"] = ">"
}
local textbox = script.Parent.TextBox
local prevtext = ""
local conn
function createconnection()
conn = textbox:GetPropertyChangedSignal("Text"):Connect(function()
conn:Disconnect()
local pos = textbox.CursorPosition
if string.len(textbox.Text) > string.len(prevtext) then
local typed = string.sub(textbox.Text, pos-1, pos-1)
if adds[typed] then
local newtext = string.sub(textbox.Text, 1, pos-1)..adds[typed]
if pos <= string.len(textbox.Text) then
newtext ..= string.sub(textbox.Text, pos, string.len(textbox.Text))
end
textbox.Text = newtext
end
end
prevtext = textbox.Text
textbox.CursorPosition = pos
createconnection()
end)
end
createconnection()
local Game = game
local Script = script
local UserInputService = Game:GetService("UserInputService")
local TextBox = Script.Parent
local Keys = {Quote = {"\"", "'"}, Nine = {")", ""}, LeftBracket = {"}", "]"}, Comma = {">", ""}}
local function OnInputBegan(InputObject)
if not (TextBox:IsFocused()) then return end
task.wait()
local Position = TextBox.CursorPosition
local Key = Keys[InputObject.KeyCode.Name]
if not Key then return end
local State = (UserInputService:IsKeyDown("LeftShift") or UserInputService:IsKeyDown("RightShift"))
TextBox.Text = string.sub(TextBox.Text, 1, Position - 1)..(if State then Key[1] else Key[2])..string.sub(TextBox.Text, Position)
TextBox.CursorPosition += 1
end
UserInputService.InputBegan:Connect(OnInputBegan)