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

Saving models using datastore?

Asked by 8 years ago

I'm creating a housing system where you can place furniture within the house that you own. You can place more than one instance of the same kind of furniture (assuming you own more than one of that furniture). When the furniture is placed, it is inserted into the house model. I tried to use data persistence's :SaveInstance() and :LoadInstance() methods to save the model as a whole to the player. When I wait for the player's data to load, nothing happens after my wait for data line. Here's an excerpt of my code:

game.Players.PlayerAdded:connect(function(player)
    print("PLAYER JOINED")
    if player:WaitForDataReady() then
        print("DATA READY")

The script prints that the player joined, but nothing happens after that. There is no "DATA READY" print. Is data persistence just unusable now or is it something I did wrong?

0
I've never referred to the return value of WaitForDataReady. Try calling it without a conditional statement and print("DATA READY") right after it. The wiki page says it's a bool, but it doesn't specify that it returns whether or not the data is ready. 1waffle1 2908 — 8y
0
@1waffle1 no, it still doesn't work dirty_catheter 7 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

DataPersistence is deprecated, use DataStores instead
You'll need to serialize the data for each model and then load it back later

How to use serialization for this?

Because you're using prebuilt assets, you're at an advantage here: You can save 2 properties per model, and you're done.
Requirements:

  • Your models are uniquely identifiable, and when placed can be linked back to the asset they were cloned from
  • Your models have a PrimaryPart

Saving

Assuming that all of your placed objects are under a single model or are tracked, you may use something like this (Assumes list is an array of placed models, and that 'Player' is predefined)

local DS = game:GetService('DataStoreService'):GetDataStore(tostring(Player.userId));
local stable = {};
for i=1,#list do
stable[i] = {list[i].Name,{list[i]:GetPrimaryPartCFrame():components()}};
end;
DS:SetAsync('Build', stable);

Loading

Working under the same assumptions as saving, but this time assuming that the positions translate to the same place. If you need a solution where they're relative to a specific position, I can provide you with that, but extra work is not my job.
Assumes that your models are in something predefined as ModelsContainer

local DS = game:GetService('DataStoreService'):GetDataStore(tostring(Player.userId));
local stable = DS:GetAsync('Build')
for i=1,#stable do
local nm = ModelsContainer[stable[i][1]]:Clone();
nm.Parent = workspace;
nm.CFrame = CFrame.new(unpack(stable[i][2]))
end;

I hope I helped.

Ad

Answer this question