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

How can i store models/ is storing models with datastore even possible?

Asked by 5 years ago

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

0
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. ScrubSadmir 200 — 5y
0
use values User#23365 30 — 5y
0
Store the name of the model in the data store. You cannot store userdata in data stores. User#19524 175 — 5y
0
You would store a string and use that string to load the car with the same name, you are welcome greatneil80 2647 — 5y
0
Made an answer! zblox164 531 — 5y

1 answer

Log in to vote
0
Answered by
zblox164 531 Moderation Voter
5 years ago
Edited 5 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:

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:

  1. DataStoreService

  2. CFrame math

  3. PrimaryParts

  4. SetPrimaryPartCFrame

  5. Table functions

  6. Tables and Dictionarys

  7. 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!

0
but the models will have diffrent decals and diffrent colors, is that possible to save witht ath method? ieatandisbaconhair 77 — 5y
0
Just add the decal ID along with the other values. ["IDS"] = DECEL1; DECAL2. zblox164 531 — 5y
Ad

Answer this question