Help with my Plugin script

I’m trying to achieve the following: when the player clicks on the button to open a Frame (the plugin’s button), a textBox will appear where they need to enter a number greater than 0 and less than 3. After entering a valid number, wait for 2 seconds, and clone as many Frames (Templates) as the number entered in the previous TextBox. Each template has a child textLabel named “name” and another TextBox. The player needs to enter text in that TextBox, and the entered text should match the text of the corresponding textLabel named “name.” So far, so good.

After entering valid text for all cloned Templates, wait for 0.5 seconds, and in the server script service, create a leaderstats-type script that will have as many currencies as the Templates cloned in the Templates folder. When the player exits the game, save the currencies, and when the player reenters the game, restore the currencies. However, the code currently creates a script with only one line, i.e., local DataStoreService = game:GetService("DataStoreService"). I want the code to create a script and add as many currency lines as the cloned Templates, with each currency in the leaderstats code having the same name as the text in the textLabels.

For example, if I chose the number 2, two templates would be cloned, and for one, I would set the name as “Cash,” and for the other, “Level.” After entering text in the textBox for both, a script should be created with two currencies: Cash and Level.

the problem is when it creates the leaderstats code with the currencies, otherwise everything goes well.

Can you please help with the correct grammatical form of the code? I would be grateful!

local ServerStorage = game:GetService("ServerStorage")
local ServerScriptService = game:GetService("ServerScriptService")

local StarterGui = game:GetService("StarterGui")
local gui = StarterGui:FindFirstChild("GUI")

if gui and gui:FindFirstChild("Frame") then
	local toolbar = plugin:CreateToolbar("Custom DataStore")
	local pluginButton = toolbar:CreateButton("Custom DataStore", "", "rbxassetid://8740888472")

	local frame = gui.Frame
	local frame1 = frame:FindFirstChild("Frame1")
	local textBox = frame1 and frame1:FindFirstChildOfClass("TextBox")
	local templatesFolder = frame:FindFirstChild("Templates")
	local template = templatesFolder and templatesFolder:FindFirstChild("Template")
	local isFrameOpen = false

	local texts = {}

	pluginButton.Click:Connect(function()
		print("Btn activated")

		isFrameOpen = not isFrameOpen


		frame.Visible = isFrameOpen
		if frame1 then
			frame1.Visible = isFrameOpen
		end


		textBox.Text = ""

		
		for _, child in ipairs(templatesFolder:GetChildren()) do
			if child ~= template then
				child:Destroy()
			end
		end

	
		texts = {}
	end)

	if textBox and template then
		textBox.FocusLost:Connect(function()
			local input = textBox.Text
			local number = tonumber(input)
			if not number then
				textBox.Text = "You can only put numbers"
			elseif number < 1 or number > 3 then
				textBox.Text = "The number is too low or too high"
			else
				
				textBox.Active = false

				textBox.Text = "Number validated successfully, wait 2 seconds!"
				wait(2)
				frame1.Visible = false
				for i = 1, number do
					local newTemplate = template:Clone()
					newTemplate.Position = UDim2.new(0.099, 0, 0.088 + (i - 1) * 0.3, 0)
					newTemplate.Parent = templatesFolder
					newTemplate.Visible = true

					local newTextBox = newTemplate:FindFirstChildOfClass("TextBox")
					if newTextBox then
						newTextBox.FocusLost:Connect(function()
							local text = newTextBox.Text
							if text:match("%d") then
								newTextBox.Text = "No numbers!!!"
							elseif #text < 2 or #text > 7 then
								newTextBox.Text = "Text length must be between 2 and 7"
							elseif texts[text] then
								newTextBox.Text = "This text has already been used"
							else
								local nameLabel = newTemplate:FindFirstChild("name")
								if nameLabel then
									nameLabel.Text = text
								end
								texts[text] = true
								newTextBox:Destroy()
							end
						end)
					end

					wait(0.5)
				end

			
				local dataStoreScript = Instance.new("Script")
				dataStoreScript.Name = "DataStore"
				dataStoreScript.Parent = ServerScriptService

				
				dataStoreScript.Source = [[
					local DataStoreService = game:GetService("DataStoreService")
				]]

				for text, _ in pairs(texts) do
					dataStoreScript.Source = dataStoreScript.Source .. [[
						local ]] .. text .. [[DataStore = DataStoreService:GetDataStore("]] .. text .. [[")

						game.Players.PlayerAdded:Connect(function(player)
							local leaderstats = Instance.new("Folder")
							leaderstats.Name = "leaderstats"
							leaderstats.Parent = player

							local ]] .. text .. [[ = Instance.new("IntValue")
							]] .. text .. [[.Name = "]] .. text .. [["
							]] .. text .. [[.Parent = leaderstats

							local saved]] .. text .. [[ = ]] .. text .. [[DataStore:GetAsync(player.UserId)
							if saved]] .. text .. [[ then
								]] .. text .. [[.Value = saved]] .. text .. [[
							end
						end)

						game.Players.PlayerRemoving:Connect(function(player)
							local ]] .. text .. [[ = player.leaderstats:FindFirstChild("]] .. text .. [[")
							if ]] .. text .. [[ then
								]] .. text .. [[DataStore:SetAsync(player.UserId, ]] .. text .. [[.Value)
							end
						end)
					]]
				end

			
				textBox.Active = true
			end
		end)
	
	end
end