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

How do you save models to datastores?

Asked by
epoke466 100
2 years ago

I am making a furniture system that saves the players furniture to a datastore when they leave the game.

I believe the problem is around line 80 when the :SetAsync() function is called. I used the datastore key editor plugin and it shows the datastore but after testing the game and checking to see if my player had data saved there was no data saved.

When the player loads into the game, it uses a system like this. All the models of furniture are located in the replicated storage in a folder called furniture. It gets the saved furniture as a table (line 25). The keys are the names of the furniture and the assigned value to each key is the position. Then it clones each one and loads them in the position, and then moves them to the correct position because the plots that the furniture is placed resembles those of tycoon games. There are multiple players, and they spawn in a random house. It needs to adapt the furniture's position to the new house, but the furniture has to have the same position relative to other furniture.

Here is the script:

local Houses = workspace.Houses
local AvailableHouses = Houses.AvailibleHouses
local UsedHouses = Houses.UsedHouses

local DataStoreServide = game:GetService("DataStoreService")
local PlayerCarsDataStore = DataStoreServide:GetDataStore("ChosenCar")
local CurrentFurnature = DataStoreServide:GetDataStore("CurrentFurnature")
local FurnatureAnchor = DataStoreServide:GetDataStore("FurnatureAnchor")
local DefaultCar = "Sedan (red)"

local function shallowCopy(original)
    local copy = {}
    for key, value in pairs(original) do
        copy[key] = value
    end
    return copy
end

game.Players.PlayerAdded:Connect(function(Player)
    local ChosenHouse = AvailableHouses:FindFirstChildOfClass("Folder")
    ChosenHouse.Name = Player.Name
    ChosenHouse.Parent = UsedHouses
    local Charachter = workspace:WaitForChild(Player.Name)
    Charachter:MoveTo(ChosenHouse.Spawn.Position)
    local playerFurnature = CurrentFurnature:GetAsync(Player.UserId)
    if playerFurnature then
        local furnatureModel = Instance.new("Model", ChosenHouse.Furnature)
        furnatureModel.Name = "FurnatureGroup"
        for key, value in pairs(playerFurnature) do
            local createdItem = game.ReplicatedStorage:FindFirstChild(key):Clone()
            createdItem.Parent = furnatureModel
            createdItem.CFrame = value
        end
        local createdAnchor = Instance.new("Part", furnatureModel)
        createdAnchor.Anchored = true
        createdAnchor.Name = "Anchor"
        local anchorValues = FurnatureAnchor:GetAsync(Player.UserId)
        createdAnchor.Position = anchorValues.Position
        createdAnchor.Orientation = anchorValues.Orientation
        createdAnchor.Size = anchorValues.Size
        createdAnchor.Shape = Enum.PartType.Ball
        createdAnchor.Transparency = 1
        createdAnchor.CastShadow = false
        createdAnchor.CanCollide = false
        furnatureModel.PrimaryPart = createdAnchor
        furnatureModel.CFrame = ChosenHouse.FurnaturePosition.CFrame
    else
        Instance.new("Model", ChosenHouse.Furnature).Name = "FurnatureGroup"
        local theeAnchor = ChosenHouse.FurnaturePosition:Clone()
        theeAnchor.Name = "Anchor"
        theeAnchor.Parent = ChosenHouse.Furnature.FurnatureGroup
    end
    game.ReplicatedStorage.Change.OnServerEvent:Connect(function(player, part, color)
        if part == "Wall" then
            for i, part in pairs(ChosenHouse.Wall:GetChildren()) do
                part.Color = color
                wait()
            end
        else
            for i, part in pairs(ChosenHouse.Trim:GetChildren()) do
                part.Color = color
                wait()
            end
        end
    end)
    Player.CharacterAdded:Connect(function(Char)
        Char:MoveTo(ChosenHouse.Spawn.Position)
    end)
    game.Players.PlayerRemoving:Connect(function(player)
        local FG = ChosenHouse.Furnature.FurnatureGroup
        local Anchor = FG.Anchor
        local AnchorProperties = {
            Position = nil,
            Orientation = nil,
            Size = nil,
        }
        AnchorProperties.Position = Anchor.Position
        AnchorProperties.Size = Anchor.Size
        AnchorProperties.Orientation = Anchor.Orientation
        FurnatureAnchor:SetAsync(AnchorProperties, Player.UserId)
        Anchor:Destroy()
        local SavingItems = {}
        for i, item in pairs(FG:GetChildren()) do
            SavingItems[item.Name] = item.CFrame do
        end
        if player == Player then
            ChosenHouse.Name = "OpenHouse"
            ChosenHouse.Parent = AvailableHouses
            end
        end
    end)
    game.ReplicatedStorage.SpawnCar.OnServerEvent:Connect(function()
        local CarName = PlayerCarsDataStore:GetAsync(Player.UserId) or DefaultCar
        local Car = game.ReplicatedStorage.Cars:FindFirstChild(CarName):Clone()
        Car.Parent = workspace
        Car:MoveTo(ChosenHouse.CarSpawn.Position)
    end)
end)
0
Tbh never saved complicated data to dss but I could help you to find out what's wrong. Add to place https://www.roblox.com/users/24398509/profile ErtyPL 129 — 2y

Answer this question