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

How do i store objects in a Datastore???

I know the basics of Datastores but i do not know how to save objects...This script is suppose to make a folder in the player, and if there are any Tools/Bricks/Models/etc...inside of that folder then insert them into a table and save them to the datastore. It does not work though so what i can i do to store and load them?

local DataStore = game:GetService("DataStoreService"):GetDataStore("BackPack")
game.Players.PlayerAdded:connect(function(player)

local Storage = Instance.new("Folder",player)

Storage.Name = "Storage" --Name of folder

local key = "user-" .. player.userId --Key
local storeditems = DataStore:GetAsync(key)

if storeditems then
	storeditems.Parent = Storage
	else
		local items = {} --Items table
		
		local get = Storage:GetChildren()-- get items
		for i, v in pairs(get) do --save tools
		table.insert(key, v)
		DataStore:SetAsync(key, items)
		
			
		end
	end
end)
		

game.Players.PlayerRemoving:connect(function(player)
	local items = {}
	local key = "user-" .. player.userId
	
	for i, v in pairs(get) do --Save tools
	table.insert(key, v)
	
	DataStore:SetAsync(key, items)
	
	end
end)

1 answer

✓ Accepted answer

Serialization

You can't store Instances. Roblox doesn't serialize them, because JSON has no support for them, and Roblox doesn't support binary data structures for storage (Truth - Roblox actually doesn't like a lot of binary values).

The solution?
Store the information which describes the Instance. Is it an IntValue? Store the number. Part? Store the information to make it, such as the name of the thing you cloned it from. Weapon? Store the name. Serialization is an important part of storage which was missed out from DataStores for some unknown reason.

2 comments

0

and how exactly i do this? i want make a script that save the object that is touching a part, and when enter, he will load this object again, how i do this?

0

and how exactly i do this? i want make a script that save the object that is touching a part, and when enter, he will load this object again, how i do this?

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