Hello fellow developers, today I encountered a challenge which I can’t seem to get over, I’m trying to find a solution.
Basically I want it to make so a textbox only allows 1 usage of a specific character,
for example if a user used (d, w, m or y) they can’t type the same character again but only for those 4 specific character, if it’s possible and you have a solution I’d be very eager to know,
thank you and have a nice day!
Not sure if this will work, never tested it. Made it in 3 minutes, it should work. Maybe. Probably. Who knows. Potentially. It might. Unsure.
--
TextBox:GetPropertyChangedSignal("Text"):Connect(function()
local Text = TextBox.Text
local LastChar = Text:sub(-1) -- -1 returns last character of the string
if LastChar == "d" or LastChar == "w" or LastChar == "m" or LastChar == "y" then -- pretty messy
local Chars = #Text:gsub("[^" .. LastChar .. "]", "") -- if this is equal or greater than 2, that means that the last character that the user put into the textbox is a duplicate
if Chars >= 2 then -- at first i forgot to add this lmao
TextBox.Text = Text:sub(1, -2) -- removes the last character that the user put
end
end
end)
2 Likes
i can provide you a function i made that would help you to solve that issue
local function DenyChars(textBox)
local restrictedChars = {d = true, w = true, m = true, y = true}
local usedChars = {}
textBox:GetPropertyChangedSignal("Text"):Connect(function()
local newText = textBox.Text
local cursorPosition = textBox.CursorPosition
local modifiedText = ""
local cursorOffset = 0
for i = 1, #newText do
local char = newText:sub(i, i):lower()
if restrictedChars[char] then
if not usedChars[char] then
usedChars[char] = true
modifiedText = modifiedText .. char
else
cursorOffset = cursorOffset - 1
end
else
modifiedText = modifiedText .. newText:sub(i, i)
end
end
if modifiedText ~= newText then
textBox.Text = modifiedText
textBox.CursorPosition = math.max(0, cursorPosition + cursorOffset)
end
end)
end
for example
local Text = script.Parent.TextBox
DenyChars(Text)
2 Likes
It works but it doesn’t allow you to use the character at all, thanks for the help tho appreciate it!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.