Item generation inside a list, using a tables

Hello DevForum Community!


Today, I'm looking for help, I've struggled with this so it would be appreciated if you could give me ideas for the solution! I'm trying my best to explain this, although, I might be not the best explainer out here.
What I'm trying to achieve is to make a list using tables ( {} ), I've finished the list and the generation but what I'm trying to achieve now is to:
  • Each item in the list has its own value.
  • But for the item values, the user must type the value into a textbox.

So like, you start with an Item, let's say Item1, I've already set its value in the script (But however in the real code I want to make it so you have to type the value first if there are 0 items in the list then press the Add button for Item1 with its value to appear). Now, in-game, I press the button, Item2 appears however its value is nil, you have to type what value you want for Item2 into the TextBox, you hit the enter in the textbox which inserts a value into Item2 and also erases the Textbox's text. And so on...

The Script

It’s a localscript:

Items = { }
Items[1] = "Item1"
Funcs = {
	["Item1"] = function()
		print("Apple") -- For example our first String to print is "Apple" in Item1
	end;
	
}
Buttons = { }
List = script.Parent.ScrollingFrame

script.Parent.TextButton.MouseButton1Click:Connect(function()
for i = 1,#Items do
	Items[i+1] = "Item"..i+1 -- this adds more buttons by each presses
	Item = Instance.new("TextButton", List)
	Item.Name = Items[i]
	Item.AutoButtonColor = false
	Item.Text = " "..Items[i]
	Item.TextXAlignment = Enum.TextXAlignment.Left
	Item.TextColor3 = Color3.new(255/255, 255/255, 255/255)
	Item.Font = Enum.Font.ArialBold
	Item.FontSize = Enum.FontSize.Size14
	Item.TextScaled = true
	Item.TextWrapped = true
	Item.TextXAlignment = "Center"
	Item.TextYAlignment = "Center"
	Item.BackgroundColor3 = Color3.new(113/255, 113/255, 113/255)
	Item.BorderColor3 = Color3.new(0, 0, 0)
	Item.Size = UDim2.new(0, 108,0, 32)
	Item.Position = UDim2.new(0, 0, 0.045*(i-1), 0)
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Add.FocusLost:Connect(function()
		-- when you type inside the textbox and hit enter:
		-- insert a StringValue based on the textbox's text into the Item, each string is something different word. E.g.: Item1 is Apple, Item2 is Pear and so on	
	end)
	if Funcs[Items[i]] then
		Item.AutoButtonColor = true
		Item.MouseButton1Down:Connect(function()
			Funcs[Items[i]]()
			
		end)
	end
	table.insert(Buttons, Item)
end
end)
How it looks like & works like

(This is actually not how the UI will look like in its final form, it will look entirely different.)


Link for the example video

Anyway, here we are; we reached the bottom of this page. I would like to thank you to whoever reads this post and willing to help.
Ciao! :wave:
~ Cronley

I would facepalm so hard if the solution is actually easy

It seems that you’re not creating the value to Funcs as you’re doing it for Items. This would be rather complicated to create, since you can’t edit functions and alter them. A simply replacement, would be to change how this scripts works a little bit.

Have the Items table contain the items as keys instead of values corresponding to, and set the item you want (the fruit you want) to the key.

[quote=“Cronle_Y, post:1, topic:501367”]
rying to achieve now is to:

  • Each item in the list has its own value.
  • But for the item values, the user must type the value into a textbox.

So like, you start with an Item, let’s say Item1, I’ve already set its value in the script (But however in the real code I want to make it so you have to type the value first if there are 0 items in the list then press the Add button for Item1 with its value to appear). Now, in-game, I press the button, Item2 appears however its value is nil, you have to type what value you want for Item2 into the TextBox, you hit the enter in the textbox which inserts a value into Item2 and also erases the Textbox’s text. And so on…

Items = { }
Items["Item1"] = "Apple"

Buttons = { }
List = script.Parent.ScrollingFrame
local textbox = --this is the box that will contain the item name

script.Parent.TextButton.MouseButton1Click:Connect(function()
for i = 1,#Items do
        local key = "Item"..i
	Items[key+1] = textbox.Text -- add the textbox's text to that item
        textbox.Text = "String Name" --then clear the name that was put if you want
	Item = Instance.new("TextButton", List)
	Item.Name = Items[key]
	Item.AutoButtonColor = false
	Item.Text = " "..Items[key]
	Item.TextXAlignment = Enum.TextXAlignment.Left
	Item.TextColor3 = Color3.new(255/255, 255/255, 255/255)
	Item.Font = Enum.Font.ArialBold
	Item.FontSize = Enum.FontSize.Size14
	Item.TextScaled = true
	Item.TextWrapped = true
	Item.TextXAlignment = "Center"
	Item.TextYAlignment = "Center"
	Item.BackgroundColor3 = Color3.new(113/255, 113/255, 113/255)
	Item.BorderColor3 = Color3.new(0, 0, 0)
	Item.Size = UDim2.new(0, 108,0, 32)
	Item.Position = UDim2.new(0, 0, 0.045*(i-1), 0)
	game.Players.LocalPlayer.PlayerGui.ScreenGui.Add.FocusLost:Connect(function()
		-- when you type inside the textbox and hit enter:
		-- insert a StringValue based on the textbox's text into the Item, each string is something different word. E.g.: Item1 is Apple, Item2 is Pear and so on	
	end)
	if Funcs[Items[i]] then
		Item.AutoButtonColor = true
		Item.MouseButton1Down:Connect(function()
			Funcs[Items[i]]()
			
		end)
	end
	table.insert(Buttons, Item)
end
end)
1 Like

Your idea seems imaginative but didn’t work, sadly, it doesn’t even add the first item to the list (maybe because of the structure). However, I wrote a code that nearly looks like according to your version. Which now looks like this however it needs to be polished.

Items = { }
Buttons = { }
List = script.Parent.ScrollingFrame
Items[1] = ""

game.Players.LocalPlayer.PlayerGui.ScreenGui.Add.FocusLost:Connect(function()
	for i = 1,#Items do
		Items[i+1] = script.Parent.Add.Text -- this adds more buttons by each presses
		Item = Instance.new("TextButton", List)
		Item.Name = script.Parent.Add.Text
		Item.AutoButtonColor = false
		Item.Text = script.Parent.Add.Text
		Item.TextXAlignment = Enum.TextXAlignment.Left
		Item.TextColor3 = Color3.new(255/255, 255/255, 255/255)
		Item.Font = Enum.Font.ArialBold
		Item.FontSize = Enum.FontSize.Size14
		Item.TextScaled = true
		Item.TextWrapped = true
		Item.TextXAlignment = "Center"
		Item.TextYAlignment = "Center"
		Item.BackgroundColor3 = Color3.new(113/255, 113/255, 113/255)
		Item.BorderColor3 = Color3.new(0, 0, 0)
		Item.Size = UDim2.new(0, 108,0, 32)
		Item.Position = UDim2.new(0, 0, 0.045*(i-1), 0)
		table.insert(Buttons, Item)
	end
end)

So like at first, I set the first item (index is 1) in the table which is empty (Maybe would be smarter if you detect for e.g.: if the first element in the table is empty or the table is empty). Also what I did too is I removed the button, and now it runs the function if you type inside the textbox and hit enter. The problem is now, if I make more than 1 item and for example I change the name for the second item, the first item’s name changes too which in this case it shouldn’t change (probably the code shouldn’t look like this, I did something stupid maybe). Click on this to see how it works now, instead of seeing it theoretically

Tables are weird

Anyway, I found the solution myself and with a little help from other people for this. I put out the Loop to avoid renaming the whole table. :slightly_smiling_face:
If someone reads this post after the solution, I hope if you needed a resource like this, it helps you to make it.

Finished code
Items = { }
Buttons = { }
List = script.Parent.ScrollingFrame
Items[1] = ""

game.Players.LocalPlayer.PlayerGui.ScreenGui.Add.FocusLost:Connect(function()
 	Items[#Items+1] = script.Parent.Add.Text -- this adds more buttons by each presses
 	Item = Instance.new("TextButton", List)
  	Item.Name = script.Parent.Add.Text
 	Item.AutoButtonColor = false
  	Item.Text = script.Parent.Add.Text
  	Item.TextXAlignment = Enum.TextXAlignment.Left
  	Item.TextColor3 = Color3.new(255/255, 255/255, 255/255)
  	Item.Font = Enum.Font.ArialBold
  	Item.FontSize = Enum.FontSize.Size14
  	Item.TextScaled = true
  	Item.TextWrapped = true
  	Item.TextXAlignment = "Center"
  	Item.TextYAlignment = "Center"
  	Item.BackgroundColor3 = Color3.new(113/255, 113/255, 113/255)
  	Item.BorderColor3 = Color3.new(0, 0, 0)
  	Item.Size = UDim2.new(0, 108,0, 32)
  	Item.Position = UDim2.new(0, 0, 0.045*(#Items-1.9), 0)
  	
	table.insert(Buttons, Item)
end)

Bye! :wave: