So I want to make a textbox that will only allow a single dash (-), and 3 numbers totally in a total maximum of 4 characters. How could I achieve this?
In otherwords I need a number-only textbox that will allow a maximum of 4 characters, one of which needs to be a dash (-)
Currently I have the following script that only allows numbers in the textbox
I have tried looking at the strings post on the documentation but struggled to understand it. Strings | Documentation - Roblox Creator Hub
script.Parent.CallsignFrame.CallsignInput:GetPropertyChangedSignal("Text"):Connect(function()
local Text = script.Parent.CallsignFrame.CallsignInput.Text
if Text:len() <= 4 then
local dash = false
for i = 1, Text:len() do
local letter = Text:sub(i,i)
if letter == "-" and dash == false then
dash = true
elseif dash == true then
break
elseif not tonumber(letter) then
break
end
end
end
end)
This should work. you might have to add a system to tell the player that the thing did not go through or you could add something different because this might not fit your needs.
Using your script I came up with this that will limit the characters in the textbox and only allow one dash and numbers as requested.
script.Parent.CallsignFrame.CallsignInput:GetPropertyChangedSignal("Text"):Connect(function()
local Text = script.Parent.CallsignFrame.CallsignInput.Text
if Text:len() <= 4 then
local dash = false
for i = 1, Text:len() do
local letter = Text:sub(i,i)
if letter == "-" and dash == false then
dash = true
print("dash used")
elseif dash == true and letter == "-" then
print("already a dash")
script.Parent.CallsignFrame.CallsignInput.Text = Text:gsub(letter, "")
elseif not tonumber(letter) then
script.Parent.CallsignFrame.CallsignInput.Text = Text:gsub(letter, "")
end
end
else
script.Parent.CallsignFrame.CallsignInput.Text = Text:sub(1,4)
end
end)