Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Cannot convert table when saving to a DataStore?

I get this error : Cannot convert mixed or non-array tables: keys must be strings on this line

datastore:SetAsync(player.UserId,data2)

Yet I have similar scripts that save similar tables which work. Any ideas? Whole code:

function save(player,data)
		local data2={
	["Bed"]=0,
	["Chest"]=0,
	["Fireplace"]=0,
	["Foundations"]=0,
	["Furniture"]=0,
	["Walls&Roof"]=0,
	["Wooden Structure"]=0	
}
	for i,v in pairs(data) do
		data2[v] = 1
	end
	local datastore = game:GetService("DataStoreService"):GetDataStore("Housing")
	datastore:SetAsync(player.UserId,data2)
end


--PLAYER LEAVES---------------
game.Players.PlayerRemoving:connect(function(player)
	
	local data= player.HasTycoon.houseBuilds:GetChildren()
	if data then
		save(player,data)
	end
end)


EDIT: as suggested this line

datastore:SetAsync(player.UserId,data2)

was updated to

datastore:SetAsync(tostring(player.UserId),data2)

but the problem persists with the same error message

11 comments

0

The key has to be a string, so try doing: key="user_"..player.UserId

0

I dont get it, would you explain a bit more?

0

UserId is a int and not a string, you could use tostring() to convert it.

0

Oh, I did but I still get the same error

0

Can you also update the code above.

0

Updated

0

What does houseBuilds hold?

0

houseBuilds is a folder that contains BoolValues named after the parts builts

2

The problem is that a data store cannot save an object you will need to go into each of these folders and get the value and put it into your table.

0

This solved the problem many thanks!

1

Can you add to the title [answered] or [closed] it just helps other ppl

1 answer

The problem here is that you are trying to save a table to DataStore, which is an unsupported data type.

The only way around this is to use a "compiler" to translate the entire table into one long string. The best way to go about this is by using HTTPService and the functions JSONEncode and JSONDecode. Just before you use the SetAsync() function, add the following line:

data2=game:GetService("HTTPService"):JSONEncode(data2)

This compiles your table into an easy-to-save string. Then, when it's time to load the data, use JSONDecode() and you'll wind up with the exact same table you started with.

I hope this helped!

2 comments

0

I still get the same error but instead of being on the SetAsync() line its on the data2=game:GetService("HttpService"):JSONEncode(data2) line

2

A table is a supported data type, the problem is that the table contains instances which are not supported. A data store will save the table as JSON any way so there is no need to convert it as it is handled for you.

This discussion is preserved as it was. New answers, comments, and votes are closed.