i am making a car game. all i need is to know how to store a model to the game. how do i do that? what do i need to do? thanks
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:
local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService("CarSave") -- could not think of anything else local function Load(plr) local key = "plr-"plr.UserId local savedCars local success, err = pcall(function() savedCars = DataStore:GetAsync(key) end) if not success then warn("Failed to read data"..tostring(err)) return end if savedCars then for i, car in pairs(savedCars) do if car then local savedCar = game.ReplicatedStorage.Cars:FindFirstChild(car.Name):Clone() if savedCar then savedCar:SetPrimaryPartCFrame(CFrame.new(car.X, car.Y, car.Z) * CFrame.Angles(0, car.RotY, 0))) savedCar.Anchored = true savedCar.Parent = workspace end end end else local saveTable = {} for i, car in pairs (locationOfCars:GetChildren()) do if car then table.insert(saveTable, { ["Name'] = car.Name; ["X"] = car.PrimaryPart.CFrame.X; ["Y"] = car.PrimaryPart.CFrame.Y; ["Z"] = car.PrimaryPart.CFrame.Z; ["RotY"] = car.PrimaryPart.Orientation.Y -- you can add x, and z } end end local succes, er = pcall(function() DataStore:SetAsync(key, saveTable) end) if not succes then warn("Failed to over-write data"..tostring(er)) return end end end local function Save(plr) local key = "plr-"..plr.UserId local saveTable = {} for i, car in pairs (locationOfCars:GetChildren()) do if car then table.insert(saveTable, { ["Name'] = car.Name; ["X"] = car.PrimaryPart.CFrame.X; ["Y"] = car.PrimaryPart.CFrame.Y; ["Z"] = car.PrimaryPart.CFrame.Z; ["RotY"] = car.PrimaryPart.Orientation.Y -- you can add x, and z } end end local success, err = pcall(function() DataStore:SetAsync(key, saveTable) end) if not success then warn("Failed to over-write data"..tostring(err)) return end end game.Players.PlayerAdded:Connect(Load) 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:
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!