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
How can i store models/ is storing models with datastore even possible?
1 answer
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!
2 comments
but the models will have diffrent decals and diffrent colors, is that possible to save witht ath method?
Just add the decal ID along with the other values. ["IDS"] = DECEL1; DECAL2.
This discussion is preserved as it was. New answers, comments, and votes are closed.

5 comments
I'm a bit new to DataStores, but I think you can use use a datastore script with things like for i loops, and use in pairs to see if the key for each player has a value of the same name as your car model.
use values
Store the name of the model in the data store. You cannot store userdata in data stores.
You would store a string and use that string to load the car with the same name, you are welcome
Made an answer!