Hiya! I was wondering if it was possible to remove letters from strings? Like if you press backspace is removes a letter? The specific letter i’d like to remove is the letter the player has just pressed on the keyboard I added to the game (Not the players keyboard)
Yes but the thing is, I am looking for it to remove the number the player has just pressed.
local CUR = 0
local CURR = nil
local numberT = {
["1"] = nil;
["2"] = nil;
["3"] = nil;
["4"] = nil;
["5"]= nil;
["6"] = nil;
["7"]= nil;
["8"] = nil;
["9"] = nil;
["10"] = nil;
}
for _, v in ipairs(script.Parent:GetChildren()) do
if v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
print(v.Name)
CUR+=1
numberT[CUR] = v.Name
CURR = v.Name
local finalString = ''
for index, value in pairs(numberT) do
finalString ..= tostring(value)
end
print(finalString)
script.Parent.Parent.DISPLAY.Text = finalString
end)
end
end
local function removeLastLetter(x: string): string
return string.sub(x, 1, #x - 1) -- string.sub(string, startPos, endPos) Including every character in the string except for the last one
end
local function removeLastLetter(x: string): string
return string.sub(x, 1, #x - 1) -- string.sub(string, startPos, endPos) Including every character in the string except for the last one
end
-- Player presses backspace or whatever
local textLabel = script.Parent.Parent.DISPLAY
local text = removeLastLetter(textLabel.Text)
textLabel.Text = text
local function removeLastLetter(x: string): string
return string.sub(x, 1, #x - 1)
end
for _, v in ipairs(script.Parent:GetChildren()) do
local finalString = ''
if v:IsA("TextButton") then
v.MouseButton1Click:Connect(function()
print(v.Name)
CUR+=1
numberT[CUR] = v.Name
CURR = v.Name
for index, value in pairs(numberT) do
finalString ..= tostring(value)
end
script.Parent.Parent.DISPLAY.Text = finalString
end)
elseif v.Name == script.Parent.BACK.Name then
removeLastLetter(finalString)
end
end
Which also messed up the original script as it tripled the numbers every time i pressed one.
local function removeLastLetter(x: string): string
return string.sub(x, 1, #x - 1)
end
for _, v in ipairs(script.Parent:GetChildren()) do
local finalString = ''
if v:IsA("TextButton") then
if v.Name == "BACK" then
v.MouseButton1Click:Connect(function()
print(v.Name, " is the back button")
if #finalString > 0 then -- Making sure the display length is bigger than zero
if #finalString > 1 then
finalString = removeLastLetter(finalString)
else
finalString = ""
end
end
script.Parent.Parent.DISPLAY.Text = finalString
end)
else
v.MouseButton1Click:Connect(function()
print(v.Name)
CUR+=1
numberT[CUR] = v.Name
CURR = v.Name
for index, value in pairs(numberT) do
finalString ..= tostring(value)
end
script.Parent.Parent.DISPLAY.Text = finalString
end)
end
end
end
The numbers tripling would be a problem with your normal-number-button logic.
v1=script.Parent.Parent.TextBox --the text box
t1="my text-" --default text
v1.Text=t1 --set the default text to text box
script.Parent.MouseButton1Click:Connect(function()
local p1,l1,t2,l3,s1 --bunch of variables
l1=string.len(t1) --l1 is the length of your default text
l3=string.len(v1.Text) --l3 is the length of you text box's text right now at this stage
if l3~=l1 then -- if the text box text is not equal to the default text length
s1=string.sub(v1.Text,l1+1,l3-1)
--[[use string sub find the
first argument the text box text,
second the first letter to search from
l1 is you default text + 1 means anything that was added later
l3 is the current textbox text length -1 means remove the last letter
--]]
v1.Text=t1..s1--set the text to t1 your default text concatenates with s1 the modified text
end
end)
you have a back space bar? (the gui TextBox)
create a local script there and place this script inside
change v1=to the text box that the text is displayed
and change the default text to anything that you want