How to add a new letter/number when pressing a button?

Hello!

I am currently stuck with a question that I can’t seem to crack. And that is:

How do you add a new letter/number on a TextLabel when a button is pressed?

I tried searching in #resources:community-tutorials but I didn’t find anything.

Any help will be appriciated!

What do you want tell me more?

I use a keypad based on parts. And I want so that everytime I press a digit it updates the Text property.

Example:

0001

Pressed 9

00019

You can have the buttons be named the letter/number it should append to the text and add the letter based on the name of the button

Example

Button.Activated:Connect(function()
    Textlabel.Text ..= Button.Name
end)
2 Likes

Create a localscript inside The GUI With the function then you can use the UserInputService Service In Order to get the Keypad input

As I said, i’m using parts. Not textbuttons

 local texts = {"1001"; "test"; "lol";}

button.MouseButton1Click:Connect(function()
      textLabel.Text = texts[math.random(1, #texts)]
end

Oh you’re using parts, in which my idea should still apply, but rather you just use the MouseClick event instead of Activated

You can try using this example code to make that, this will also work with letters.

digit.Activated:Connect(function()
    local newAmount = #textLabel.Text + 1 -- how long the number will be now

    if newAmount >= 5 then -- if the length of the new number is over or at 5
        return
    end

    textLabel.Text ..= digit.Text -- combine the number to the textlabel's text
end)
local Gui = script.Parent

Gui.MouseButton1Click:Connect(function()
Gui.Text = tonumber(math.random())
end
1 Like

Also, do you know how I can tell if it’s at the character limit?

In terms of character limit, you could do what @aguythatsreallybored mentioned via getting the length of the current text in the label and if it’s above a certain length, do nothing.

1 Like