Max String Length

Hey developers!

I’m trying to make a text show the number of the length on a string.

For example you have 300 characters to type in a textBox and as you type the number goes down.

I’ve tried to do this

local text1 = script.Parent.Text -- This is the text box text
local text2 = script.Parent.Parent.TextLabel.Text -- This is the "300" text

while true do
	local counter = string.len(text1)
	text2 = 300 - counter
	
	wait(0.1)
end

This is a local script but it didn’t work.

I don’t know how to make this work as I’m kinda new to string stuff so any help would be appreciated!

Bonus Help?

And if possible to make the color of the text change based on the text length, starting color would be white, and then yellow and then red when it reaches a low number

Thanks in advance!

1 Like

don’t add .Text when defining text1 and Text2. Instead, just do this:

local text1 = script.Parent-- This is the text box text
local text2 = script.Parent.Parent.TextLabel-- This is the "300" text

text1.Changed:Connect(function() --Try to avoid infinite loops with wait() where possible
	local counter = string.len(text1.Text)
	text2.Text = 300 - counter
end)

as for the color changing, you could look into interpolating color3.

3 Likes