Textbox character limiter

Hi, I want to make a music gui, and I’m using a textbox, but I don’t know how to make a limiter for the characters, I tried to use two different Local Script, one for the real script and the second one for the limiter, but the game on phone lag a lot, so how can I do ?

First Local Script

TextBox.Changed:Connect(function()

	local Car1 = workspace.PlayersFolder:FindFirstChild(LocalPlayer.Name):WaitForChild("Car5", 1)
	local Audio = Car1.Body.Music.MusicAudio
	
	
	
	Audio.SoundId = "rbxassetid://"..TextBox.Text
end)

Second Local Script (Limiter):

TextBox.FocusLost:Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '')
	TextBox.Text = TextBox.Text:sub(1,10)
	wait()
end)


Try using MaxVisibleGraphemes, Should limit the Characters Visible

You dont need 2 local scripts for that. You should put it like this:

local TextBox = script.Parent
local Car1 = workspace.PlayersFolder:FindFirstChild(LocalPlayer.Name):WaitForChild("Car5", 1)
local Audio = Car1.Body.Music.MusicAudio

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

TextBox.FocusLost:Connect(function()
	Audio.SoundId = "rbxassetid://"..TextBox.Text
end)

ok it works, but what about the number filter ? , cause I want that a player can only write number so actually the script is this:

local MusicGUI = script.Parent.Parent.MusicGUI
local TextBox = MusicGUI.TextBox
local PlayButton = MusicGUI.PlayButton

local LocalPlayer = game:GetService("Players").LocalPlayer

TextBox.Changed:Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '');
	TextBox.Text = TextBox.Text:sub(1,10)
	
end)


TextBox.FocusLost:Connect(function()

	local Car1 = workspace.PlayersFolder:FindFirstChild(LocalPlayer.Name):WaitForChild("Car5", 1)
	local Audio = Car1.Body.Music.MusicAudio
	
	
	
	Audio.SoundId = "rbxassetid://"..TextBox.Text
end)



But if I use just the character limiter it works, but if I add the character filter it lag anyway

To make it so players can only write numbers use this:

TextBox.Text = TextBox.Text:gsub("[^%-%d]", "")

Move Car1 and Audio out of the function since you dont need to define them every time u call that function.

local maxCharacters = 10

TextBox.Changed:Connect(function()
	TextBox.Text = TextBox.Text:gsub("%D+", ""):sub(1, maxCharacters)
end)

it runs but the game on phone lag a lot, I don’t know why…

same thing, the game lag a lot on phone, and I don’t think is my phone (Iphone SE 2020) cause if I play Jailbreak all text boxes work

I forgot to mention to add a debounce since changing the text still fires the event:

local maxCharacters = 10
local debounce = true

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	if debounce then
		debounce = false
		TextBox.Text = TextBox.Text:gsub("%D+", ""):sub(1, maxCharacters)
		debounce = true
	end
end)

ok now I changed the scirpt like this but if I use the phone the game lag when I try to delete what I write

local MusicGUI = script.Parent.Parent.MusicGUI
local TextBox = MusicGUI.TextBox
local PlayButton = MusicGUI.PlayButton


local LocalPlayer = game:GetService("Players").LocalPlayer

local Car1 = workspace.PlayersFolder:FindFirstChild(LocalPlayer.Name):WaitForChild("Car5", 1)
local Audio = Car1.Body.Music.MusicAudio



TextBox.Changed:Connect(function()
	local maxCharacters = 10
	local debounce = true
		if debounce then
			debounce = false
		TextBox.Text = TextBox.Text:gsub("%D+", ""):sub(1, maxCharacters)
		Audio.SoundId = "rbxassetid://"..TextBox.Text
			debounce = true
		end
	
end)


You need to put the maxCharacters and debounce outside the function that’s basic common sense.