Can't save something to Datastore

So, I’ve been recently learning about DataStores, and I was trying to make a data store script where it would save data inside a folder that is inside the player. But, everytime I run the script, the Output comes up with: Argument 2 missing or nil. Does anyone know about what’s going on? By the way, here’s the code:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local Folder = Instance.new("Folder")
	Folder.Name = "Hats"
	Folder.Parent = player.Head

local data
local success, errormessge = pcall(function()
	data = myDataStore:GetAsync(player.Userid)
end)
if success then
	Folder = data
else
	print("Data Error")
	warn(errormessge)
end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId)
	end)

if success then
	print("Data fully saved")
else
	print("Data error")
	warn(errormessage)
end	

end)

1 Like

You forgot to tell the data store what to save in the PlayerRemoving function.

2 Likes

And you also can’t save Folders in DataStores. You can only save Strings, Numbers and Tables!

2 Likes

Then how do you do a data store for items like folder’s?

1 Like

The last time I saved “items” in Data Stores I created a BoolValue for every item and then I would set it’s value to true if the item was bought and keep it false if it wasn’t bought.

You can use tables to make it easier!

And I would obviously save it, using a line like this:

DataStore:SetAsync(Player.UserId .. "-ItemNameHere", BoolValue.Value)

This tutorial might help you out, @Dogcraft_01! https://www.youtube.com/watch?v=ofZT3t8msI4&t=716s

1 Like

In your set async function, you aren’t specifying what data you are trying to store. You can only store string values, and not any objects.

1 Like

I’m making a player customization system for items on your head. Do I just add a DataStore for every item that you can wear?

1 Like

I’ve already told him this, but thanks for reminding him!

You can use different keys for every save.
For example instead of doing:

local ds1 = DataStoreService:GetDataStore("1")
local ds2 = DataStoreService:GetDataStore("2")
etc.

You can simply use different keys such as these:

local key1 = Player.UserId .. "Hat1"
local key2 = Player.UserId .. "Hat2"

And you can save all of these keys in the same data store!

1 Like

Oh ok that makes sense. I’ll give this a try. Thanks. :slight_smile:

1 Like

A’ight, no problem! Good morning/afternoon/evening! Don’t forget to mark one of my suggestions as the Solution!

1 Like

Making a new datastore for each item is not a great idea, what you should do instead is pack all the items the player owns in a table/dictionary and store that instead. for example:

{
   "Item1",
   "Item2" 
}

That way when you load a player’s data you won’t have to make a ton of requests to multiple datastores but instead, make 1 request to fetch all data which then you could use to iterate over and instance StringValues for each item/keep them in a table, up to you.

That’s exactly what I told him! I told him to use different keys instead of using different data stores!

1 Like

Different keys also mean multiple requests, keep the key as the player’s userId and store all data to that key.

Doesn’t really matter because it’s still saying Argument 2 missing or nil

You probably haven’t set what is going to be saved.

Well, yeah… You’re right! 30_charss

Keep in mind you can only save Strings, Numbers and Tables!