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

How do I save and load instances with datastores?

Asked by 5 years ago

I've successfully written code to save and load a player's money and kill count. However, I'm facing significant challenges when I try to save the player's building. At first I attempted to save all of the information about the player's parts in a table, until it threw this error:

 Cannot store table in data store. Data stores can only accept valid UTF-8 characters.

I then tried saving all of the information about the part individually to different data stores like so:

local dss = game:GetService("DataStoreService")

local module = function(player)
    print("Save started...")
    local playerKey = "Player_" .. player.UserId
    for i,v in pairs(workspace[player.Name .. "Builds"]:GetChildren()) do -- loop through player's builds
        local dataStore = dss:GetDataStore("AutoSave" .. i)
        dataStore:SetAsync(playerKey .. "part", v)
        dataStore:SetAsync(playerKey .. "cframe", v.CFrame)
        dataStore:SetAsync(playerKey .. "color", v.Color)
        dataStore:SetAsync(playerKey .. "size", v.Size)
        dataStore:SetAsync(playerKey .. "material", v.Material)
        dataStore:SetAsync(playerKey .. "cost", v.Cost.Value)
    end
    print("Successfuly Saved!")
end

return module

However, it turns out that parts can't be saved into datastores either. My attempt yielded a similar error:

 Cannot store Instance in data store. Data stores can only accept valid UTF-8 characters.

I tried again by saving the name of the part and then in my load method making a clone of that part based on the name, but it turns out coordinate frames can't be stored either. Even if it would work, it requires sending 6 different requests to the server for the information about one part, and this would have to be repeated for every part. At this point I realized any further attempts would be redudant, because the error message was clearly articulating my problem: Datastores can only store strings or numbers

Therefore, how does one save data about instances? My game would greatly benefit from users being able to save their creations. I learned about the deprecated Player:SaveInstance and Player:LoadInstance, but I couldn't seem to get those to work and there's little documentation on the wiki. I've seen similar things in games going way back so I'm legitimately curious about how seemingly difficult this is. Any pointers or assistance would be appreciated.

Thank you in advance :)

0
You need to serialize the other datatypes too like CFrame by doing something like {CFrame:components()} to serialize and CFrame.new(unpack({CFrame:components()})) hiimgoodpack 2009 — 5y

Answer this question