Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Saving Instances With Data Store?

Asked by 9 years ago

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

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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)    
Ad

Answer this question