Attempt to Index nil with

Hello, i try to use UpdateAsync, but get this error:

ServerScriptService.DataStore:66: attempt to index nil with 'A'

this is the script in cuestion:

players.PlayerRemoving:Connect(function (player)
	local FolderData = player.FolderData
	datastoreservice:UpdateAsync(player.UserId, function(oldata)
		local newdata = {}
		local SUCCES, ERROR = pcall(function()
			for Data, Value in oldata do
				if type(Value) == "table" then
					for Index, Val in Value do
						newdata[Data][Index] = FolderData[Data][Index].Value
					end
				else
					newdata[Data] = FolderData[Data].Value
				end
			end
		end)
		if SUCCES then
			print("xd")
			return newdata
		else
			print(ERROR)
			return oldata
		end
	end)
end)

and the DataStore like this:

local NEWDATA = {
	["TrabajoActual"] = "aea",
	["Level"] = 0,
	["CarLicences"] = {
		["A"] = false,
		["B"] = false,
		["C"] = false,
		["E"] = false
	}
	
}

and FolderData:

error

other times appear: ServerScriptService.DataStore:66: attempt to index nil with 'TrabajoActual'
and other times doesnt appear nothing

i tried clone player folder in serverstorage and concatenate name with FolderData … player.Name and after complete the update, destroy the folder but i get the same error

i belive that problem is here: FolderData[Data][Index].Value
but i dont know why

apologies for bad english

This most probably happens because when indexing the FolderData, the instance isn’t actually created. Can you try debugging print(FolderData:GetChildren()) before you apply the value?

i get this using GetDescendants:

  19:42:40.763  TrabajoActual  -  Servidor - DataStore:61
  19:42:40.763  CarLicences  -  Servidor - DataStore:61
  19:42:40.763  A  -  Servidor - DataStore:61
  19:42:40.763  B  -  Servidor - DataStore:61
  19:42:40.763  C  -  Servidor - DataStore:61
  19:42:40.763  E  -  Servidor - DataStore:61
  19:42:40.764  Level  -  Servidor - DataStore:61

Well it shouldn’t have any problems then. On which line is your code erroring?

My bad, I think I found the problem.

You’re creating the newData table and looping over it. If the value is a table you loop over it again and apply to newdata[Data][Index]. However, the newdata[Data] is nil. Before you apply the main one, you have to do

newdata[Data] = {}

…to actually make it a table where you can define indexes

I has another question, in the second for loop, the Data will not put in newdata table? because in the first loop take Data index for the second loop

			for Data, Value in oldata do -- You loop through the main table
				if type(Value) == "table" then -- if the value's type is `table` then
					for Index, Val in Value do -- loop through the `value`
						newdata[Data] = {} -- have to define a table for the `Data` index or else it will stay `nil` which is why you're getting the error
						newdata[Data][Index] = FolderData[Data][Index].Value -- THEN, we apply the `Index` index to the `Data` index
					end
				else -- if the type isn't a table, you don't need to make a table, just apply the value
					newdata[Data] = FolderData[Data].Value
				end
			end

thanks you very much :partying_face:

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