15: invalid argument #1 to 'pairs' (table expected, got string)

So i was Trying to Make the Color of the Parts save But then it told me Arrays Can’t be stored so i used a way to save them Then when it is getting the Data it gave me that Error i would be Grateful if anyone can help. Thank you and have a Good day!

Code:

local DataStore = game:GetService("DataStoreService")
local Data = DataStore:GetDataStore("Data471112")
local RS = game:GetService("ReplicatedStorage")
local Builder = RS:WaitForChild("Builder")
local function GetBase(player)
	coroutine.resume(coroutine.create(function()
	for i,v in pairs(script.Parent:GetChildren()) do
		if v:IsA("Model") and v.Player.Value == "" then
			v.Player.Value = player.Name
				local Items = Data:GetAsync(player.UserId)
				
			print("Step1")
			if Items then
				print("Step2")
				for i,item in pairs(Items) do
					print("found")
					local ItemName = item[1]
					print(ItemName)
					print("found0.5")
					local CFm = CFrame.new(unpack(item[2]))
					print("found1")
					local CloneItem = Builder:FindFirstChild(ItemName):Clone()
					print("found2")
					CloneItem.Parent = v.ItemHolder
					print("found3")
					local nCFm = v.Place.CFrame:ToWorldSpace(CFm)
						print("found4")
						
					if CloneItem:IsA("Model") then
						print("Isa model")
						CloneItem:SetPrimaryPartCFrame(nCFm)
						else
							
							local color = BrickColor.new(unpack(item[2]))
							CloneItem.CFrame = nCFm	
							
					end
				end
			end
			return
		end
	end
	end))
end

local function GrabBase(player)
	coroutine.resume(coroutine.create(function()
	for i,v in pairs(script.Parent:GetChildren()) do
		if v:IsA("Model") and v.Player.Value == player.Name  then
			v.Player.Value = ""
			print("Mwoaw")
				local Items = {}
				
			for i,item in pairs(v.ItemHolder:GetChildren()) do
	
				if item:IsA("Model") then
											
					print("Model")
					local CFm = v.Place.CFrame:toObjectSpace(item.PrimaryPart.CFrame)
					table.insert(Items,{item.Name,{CFm:components()}})
					item:Destroy()
					else
						print("Part")
						local Color = item.BrickColor
					local CFm = v.Place.CFrame:toObjectSpace(item.CFrame)
						table.insert(Items,{item.Name,{CFm:components()},{Color}})
					
					item:Destroy()		
	end
			end
				wait()
		
		Data:SetAsync(player.UserId,game:GetService("HttpService"):JSONEncode(Items))


		end
	end
	end))
end

game.Players.PlayerAdded:Connect(function(Player)
	GetBase(Player)


end)

game.Players.PlayerRemoving:Connect(function(Player)
	GrabBase(Player)
end)

1 Like

ok so i tried changing

for i,item in pairs(Items)

to

for i,item in pairs{Items}

the error is gone but now everything is nil

Keep it with the normal curl brackets.

You’re also trying to loop through something that isnt a table, make sure to read your errors.

with

for i,item in pairs(Items)

i get an error that says invalid argument #1 to ‘pairs’ (table expected, got string)
with

for i,item in pairs{Items}


no errors but it prints everything as nil
and can you explain in depth what u mean by something that isn’t a table im new to arrays and stuff sorry if i don’t understand some stuff you sat

Use the normal brackets, and as i said, you’re trying to loop through something that isnt a table.

If you use the curly brackets you’re just making the script loop trhought something that isnt defined hence why it returns nil

2 Likes

Alright so i did that but why does it say it is a string

Okay, ill go a bit more in depth:

You defined Items as the datastore:GetAsync bit, but that obviously doesnt return a table as you probably haven’t saved a table to the datastore yet. A table can be anything: like {1; 2; 3} to even Players:GetPlayers()

(GetPlayers returns an array of names)

I also have no idea what you’re trying to achieve with your code, so maybe elaborate a bit?

So what I have is a plot saving script and in the building system I have u can customize that build and change its colours and material the plot saving was working fine until I tried to make it save the colour

This is nothing to do with your error directly but may be related to why your getting some weird data saving / loading.

I believe you cant store color3 values, so its best to store it as an integer attached to a string / binary number defining if its ‘R’, ‘G’, ‘B’ to prevent errors.

2 Likes

Is there other ways to saving a color3 value

Oh I realised what you meant sure thing will try that

Items is a string, not a table. This means that you’re saving data as a string.

This is the line that saves data. You’re using HttpService:JSONEncode() and it returns a string. Maybe try another way to save data.

1 Like

Don’t JSONEncode tables before saving, they get internally encoded to strings when saving so you’re just doubling the amount of data you’re saving.

The issue might be that you didn’t decode the JSONEncode’d data, so you were essentially trying to iterate through a string with pairs.

Yes like you and @GalaxyGourmet said I used to have it Data:SetAsync(player.UserId , Items) but it told me you cant save arrays so I used this way but I think why this is happening because I was saving the color3 value the wrong way

Because when I remove the colour thing in the table it works like before which saves the CFrame and the plot

What told you you can’t, it is possible to save tables to DataStores without any encoding.

Just don’t encode the table before saving it.

Yeah i did it but Now i need to find a way to make the BrickColor Save

Thanks It Worked this is what i did

in saving the Color i added this

table.insert(Items,{item.Name,{CFm:components()},{item.Color.R,item.Color.G,item.Color.B}})

and then while getting the data and cloning the item i Added This

CloneItem.BrickColor =  BrickColor.new(Color3.new(unpack(item[3])))

Thanks For the Help and Thanks Every One :smiley:

1 Like