Answered by
6 years ago Edited 6 years ago
Very simple solution!
The solution
You would simply save the name of the model then when you need it again just make a clone of the model. To save the position of the model you need to create a dictionary which saves the models coordinates. To save the correct model you need to loop around all the models you want to save and get the names. I will give an example of what I mean by saving names (and getting all the required models), and saving the coordinates below.
Example:
01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 | local DataStore = DataStoreService( "CarSave" ) |
04 | local function Load(plr) |
05 | local key = "plr-" plr.UserId |
09 | local success, err = pcall ( function () |
10 | savedCars = DataStore:GetAsync(key) |
14 | warn( "Failed to read data" .. tostring (err)) |
19 | for i, car in pairs (savedCars) do |
21 | local savedCar = game.ReplicatedStorage.Cars:FindFirstChild(car.Name):Clone() |
24 | savedCar:SetPrimaryPartCFrame(CFrame.new(car.X, car.Y, car.Z) * CFrame.Angles( 0 , car.RotY, 0 ))) |
26 | savedCar.Anchored = true |
27 | savedCar.Parent = workspace |
34 | for i, car in pairs (locationOfCars:GetChildren()) do |
36 | table.insert(saveTable, { |
39 | [ "X" ] = car.PrimaryPart.CFrame.X; |
40 | [ "Y" ] = car.PrimaryPart.CFrame.Y; |
41 | [ "Z" ] = car.PrimaryPart.CFrame.Z; |
42 | [ "RotY" ] = car.PrimaryPart.Orientation.Y |
47 | local succes, er = pcall ( function () |
48 | DataStore:SetAsync(key, saveTable) |
52 | warn( "Failed to over-write data" .. tostring (er)) |
58 | local function Save(plr) |
59 | local key = "plr-" ..plr.UserId |
63 | for i, car in pairs (locationOfCars:GetChildren()) do |
65 | table.insert(saveTable, { |
68 | [ "X" ] = car.PrimaryPart.CFrame.X; |
69 | [ "Y" ] = car.PrimaryPart.CFrame.Y; |
70 | [ "Z" ] = car.PrimaryPart.CFrame.Z; |
71 | [ "RotY" ] = car.PrimaryPart.Orientation.Y |
76 | local success, err = pcall ( function () |
77 | DataStore:SetAsync(key, saveTable) |
81 | warn( "Failed to over-write data" .. tostring (err)) |
86 | game.Players.PlayerAdded:Connect(Load) |
87 | game.Players.PlayerRemoving:Connect( function (Save) |
This process is called serialization. Serialization is where you take an object and convert it to a string (in this case a table) and save it. Then re-create it when needed. It's quite simple when known and understood but can be hard to grasp (it was for me at least).
You will want to make the cars have PrimaryParts so the script will work.
Here is some info on the topics used in this answer:
DataStoreService
CFrame math
PrimaryParts
SetPrimaryPartCFrame
Table functions
Tables and Dictionarys
Developer Wiki (anything else you might need)
All code is untested but the idea has been used me and another devs sandbox tycoon-ish game and it works fine (This code is not exactly the same though).
You will want to save at these times: When the player leaves, when the player clicks the save button (if you have it), when they purchase something, and every x seconds.
Hope this helps!