My Table is not working properly

Hello scrippter, I am making a system that collects the information of some folders and converts them into Buttons, my problem is that when creating a table to organize my buttons I get this error

Unable to assign property Name. string expected, got nil

this is a part of the script

table.clear(DifficulyTable)
for _,cd in pairs(SongFolder[ModFrameSchemeClone.Name].Songs[s.Name].Difficulties:GetChildren()) do
	wait()
	table.insert(DifficulyTable,cd.Order.Value ,cd.Name)
end

local Order = 0
for _,d in pairs(DifficulyTable) do
	local DifficultieFrameSchemeClone = DifficultiesFrameScheme:Clone()

	DifficultieFrameSchemeClone.Name = DifficulyTable[Order]
	DifficultieFrameSchemeClone.Visible = true
	DifficultieFrameSchemeClone.Parent = DifficultiesFrameScheme.Parent
	DifficultieFrameSchemeClone.Position = UDim2.new(DifficultiesFrameScheme.Position.X.Scale, 0, OldDifficultiesPositionY, 0)

	DifficultieFrameSchemeClone.TextButton.Text = DifficulyTable[Order] 

	OldDifficultiesPositionY = OldDifficultiesPositionY + 0.11

	Order += 1
end
1 Like

Arrays in Lua don’t start from 0, they start from 1. You don’t even need to make a variable to do this, you can just replace DifficulyTable[Order] with d because you’ve defined d in the for loop

for _,>>>d<<< in pairs(DifficulyTable) do
2 Likes

To clarify this a bit, your for loop is already giving you pairs of information.

When you specify a paired loop like for i,v in pairs(TableVariable) do

  • i is your integer from an array OR i is the key from a dictionary. (Both can be referred to as Index)
  • v is your value

By using a paired loop, you are basically completing a localized list of commands/function for every pair of data. In your case, since DifficulyTable is an array, you have _ representing your integer variable and d representing your value variable.

Here’s an example dictionary and how a paired loop would use those variables.

local DictionaryExample = {
--	["Key"]          = Value
	["NumberTest"]   = 123
	["BoolTest"]     = false
	["InstanceTest"] = Instance.new("Part")
}

for i,v in pairs (DictionaryExample) do  --Each time this loop runs, it will supply a key (Variable i) with it's accompanying value (Variable v)
--> During one of the runs on the loop, i would be equal to the string "NumberTest" and v would equal the number 123.
--> During a different run, i would be equal to the string "BoolTest" and v would equal false.
--> etc.
end

Indexing an array with an integer, returns the value of what is in that placement in the array.
For example:

ArrayExample = { 123, false, Instance.new("Part") }
--ArrayExample[0] -> nil
--ArrayExample[1] -> 123
--ArrayExample[2] -> false
--ArrayExample[3] -> Part

Using a paired loop on an array already gives you it’s current placement in the form of an integer so you don’t have to create or increment your own variable (Order).

for i,v in pairs (ArrayExample) do -->This goes through every value in the table in order.
--Variable i would be equal to v's placement in the array which happens to also be the current pass on the loop.
--For example (with this specific table): If v were to equal Part then i would be 3. If v were to equal 123 then i would be 1.
end

Because you are already receiving the keys and values from your script’s loop, you don’t need to index the table again with Order, especially since during your first pass of the loop, you indexed it with 0 (which returned nil) resulting in an error when you tried to set a string property (Name) to nil. You can just reference the variable you made (d) that already represents the value of the loop’s current position in the array.

DifficultieFrameSchemeClone.Name = DifficulyTable[Order]
DifficultieFrameSchemeClone.Name = d

1 Like

You are absolutely right, when I did this I was very tired and I think if you can tell that I was tired, thank you