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

Saving Instances With Data Store?

On the wiki of datastores, It says Saves arbitrary lua tables, besides strings, booleans and numbers. Instances cannot be saved directly though. How would i save it (indirectly)?

1 answer

Although you can't directly save Instances to DataStores, there's an alternative.. when a player leaves save a table full of the names of the parts you want to save. Then upon a player entering iterate through that table, finding and cloning any matches you find from ServerStorage. You'd have to put all possible parts in ServerStorage.

Example;

local ds = game:GetService('DataStoreService'):GetDataStore('Instances')
local holder = game.SeverStorage

game.Players.PlayerAdded:connect(function(plr)
	local data = ds:GetAsync(plr.userId)
	for i,v in pairs(data or {}) do --'or {}' is in case there's no data
		local part = holder:FindFirstChild(v)
		if part then
			part:Clone().Parent = workspace
		end
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local data = ds:GetAsync(plr.userId)
	if data ~= nil then
		ds:UpdateAsync(plr.userId, function(old) return {"Part","Part1","Part2"} end)
	else
		ds:SetAsync(plr.userId, {"Part","Part1","Part2"})
	end
end)	

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