[SOLVED] My text box isn't limiting the letters no matter what

Hi developers,

I have tried making a game, but for some reason my text box is not limiting the amount of letters, i have tried so much on the developer hub, and mainly i tried using this script:

local TextBox = script.Parent

TextBox.Changed:Connect(function()
	TextBox.Text:sub(1, 2)
end)

The weird part is that for some reason, in my other games that worked perfectly all the time, but now on this specific game, it’s not working, can anyone help me? Any help would be appreciated.

Thank you.
– Heitorsonic123508 (no it’s not laheitorzito303030, this is my alt account)

1 Like

The problem with your code is your not setting the Text of the textbox.

Try this code:

local TextBox = script.Parent

TextBox.Changed:Connect(function()
	TextBox.Text = TextBox.Text:sub(1, 2)
end)
2 Likes

lemme see if it works, i will be very grateful if it works

Thanks, it works!!! Now just a bonus, how would i limit it for only numbers, and only a certain number? like 12?

1 Like

Im not so sure what you mean, can you elaborate / give an example?

1 Like

Look, i mean, there’s a text box, but i can only input a max of number to 12 instead of 99 and no limits and stuff, sorry if i’m bad at explaining

So you mean: A textbox were you are only allowed to input numbers upto a certain maximum?

2 Likes

Exactly! Well, actually i think i limited only to numbers, just the number maximum thing now

1 Like

Um, why do you just disappeared lol

1 Like

This code should work for integers (whole numbers)

local TextBox = script.Parent
local Max = 12 -- Max value 12

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	local str = TextBox.Text:gsub("%D+", "") -- Only allows for numbers (0-9)
	local number = tonumber(str)
	if number then
		TextBox.Text = math.min(number, Max)
	else
		TextBox.Text = ""
	end
end)
2 Likes

What is integers? (Sorry, i don’t know what is that)

Also, it works, thank you!

Seriously, why didn’t they removed that?

1 Like

Oh i want to ask one final thing, how can i make when the player inputs a value > than 4, how can i make the text red?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.