Table.insert() Not working

Hello, I have some code here that is meant to get the children of a folder in the player and insert the name of each child into an array and for some reason the for i loop is not working. Everything else prints and the functions afterwards work just fine. I also have a couple functions nearly identical to this one that work just fine so I’m really lost and any help is very much apricated.

if Player:FindFirstChild('AutoDeleteFolder') then
		print('Auto delete folder exists')
		local AutoDeleteArray = {}

		for i, v in pairs(Player.AutoDeleteFolder:GetChildren()) do
			print(v.Name)
			table.insert(AutoDeleteArray, v.Name)
		end
		
		print(AutoDeleteArray)
		local success, errormessage = pcall(function()
			DataStore:SetAsync(Player.UserId..'-autodelete', AutoDeleteArray)

			if success then
				print('saved auto delete!')
			end

		end)
	end
1 Like

What do you exactly mean when you say the the for i loop isn’t working?
Is it throwing an error or are the values not being stored in the table?

Nah no errors or anything just the print and the table.insert wont work. I have no clue why either because I use a function nearly identical to this one just with different variables to save other data and it works just fine.

1 Like

Does it print “Auto delete folder exists”? What is in the auto-delete folder? How is the folder created?

1 Like

Maybe there is nothing in the folder?
You could also try to switch to ipairs() instead of pairs() but i don’t think that would make a difference

Yes everything prints apart from whats in the for loop, there is just some string values inside the folder and the folder is created by a server script when a player joins the game.

image
image

1 Like

It won’t make a difference in this case. Most of the time, including this time, pairs and ipairs isn’t even needed, it can just be written like:

for i, v in Player.AutoDeleteFolder:GetChildren() do
1 Like

Ill give that a try and just see what happens.

When does this code run? Are you sure it isn’t checking the auto-delete folder before anything has been added to it?

This code only runs when a player leaves the game or the server is shutdown.
image

The issue is that the script doesn’t see what is inside the folder.

Are you using Instance.new('Folder') or are you just parenting an already existing folder to the Player?

1 Like

If you switch to the Server view while in a play test and use the Explorer while in that mode, is the folder still there?

Yes it is, but that tells me why its not working lmao im so sorry for wasting your guys time, someone else wrote the code that puts the stringvalues into the folder on a local script so in the server the folder is empty :skull:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.