How do i insert items into folder using a table?

  1. What do you want to achieve? i want to put tools into the player’s saved items folder and save it when something is added into the folder (like a tool)

  2. What is the issue? it doesnt work for some reason?

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub? yes i did but i dont understand any of them

here’s some of the code

local function addSavedItems(player)
	local allIngredientsFolder = game.ServerStorage.ingredientsFolder
	local playerData = sessionData[player.UserId]['unlockedItems']
	
	if not sessionData then
		addSavedItems()
		return
	end
	
	local playerIngredientsFolder = player:FindFirstChild('savedItems')
	
	for i, tool in pairs(allIngredientsFolder:GetChildren()) do
		if tool:IsA('Tool') then
			if playerData[tool.Name] then
				print('loaded ', playerData)
				local toolClone = tool:Clone()
				toolClone.Parent = playerIngredientsFolder
			end
		end

	end
end

local function saveItems(player)
	local playerIngredientsFolder = player:WaitForChild('savedItems')
	local playerData = sessionData[player.UserId]['unlockedItems']
	
	for i, tool in pairs(playerIngredientsFolder:GetChildren()) do
		if tool:IsA('Tool') then
			if not playerData[tool.Name] then
				table.insert(playerData, tool.Name)
			end
			
		end
	end
	
	print(playerData)
end
	
 --]]
local function playerAdded(player)
	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'leaderstats'

	local tix = Instance.new('IntValue')
	tix.Name = 'tix'
	tix.Value = 100
	tix.Parent = leaderstats

	local savedItems = Instance.new('Folder')
	savedItems.Name = 'savedItems'--]]
	
	local success = nil
	local playerData = nil
	local attempt = 1
	
	repeat
		success, playerData = pcall(function()
			return dataBase:GetAsync(player.UserId)
		end)

		attempt = attempt+1

		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5
	
	if success then
		print(player.Name..' connected to database')
		if not playerData then
			print('assigning default data for '..player.Name)
			playerData = {
				['tix'] = 100;
				['unlockedItems'] = {'bread','vegetable','meat'}
			}

		end
		
		sessionData[player.UserId] = playerData
		
	else
		warn('unable to get data for '..player.Name)
		player:Kick('Unable load your data. Please try again later')
	end
	
--this part is where i call the function
	addSavedItems(player)
	
	tix.Value = sessionData[player.UserId].tix
	tix.Changed:Connect(function()
		sessionData[player.UserId].tix = tix.Value
	end)
	
	leaderstats.Parent = player
	savedItems.Parent = player
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Your recursively calling the function for no reason which will make no difference, did you mean to do this?

do i put it where it originally was?

Completely remove it..

how do i call the function now?

The function is called somewhere else in the code, calling itself for no reason will just error and stop it from working.

thanks for the help! i made a bit of tweaks in my script again and now it works

Your welcome, glad to have helped.

1 Like

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