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

Saving And Loading A Grid Placement Systems Assets?

Asked by 6 years ago
Edited 6 years ago

Hey. Ive been wanting to make a grid placement sandbox tycoon, but im not sure how to save models locations in the base. What im mainly unsure of is how to save all the instances positions and load them all in one datastore, would i do it all in one table? how would i know how many objects are being loaded? Or am i overcomplicating this? Any help is appreciated, thanks!

Ive been given the idea of this script but through testing it doesnt seem to be working and comes up with no errors. Do i need to enable save place api?

DATA = game:GetService("DataStoreService"):GetDataStore("OINSDHLSJHG")

game.Players.PlayerAdded:connect(function(plr)
    local t = Instance.new('StringValue')
    t.Name = "Tycoon"
    t.Parent = plr
    if workspace["Tycoon A"].Owner.Value == "" then
        local baseplate = workspace["Tycoon A"].Base
        plr.Tycoon.Value = "Tycoon A"
        workspace["Tycoon A"].Owner.Value = plr.Name
    end
    plr:WaitForDataReady()
    if DATA:GetAsync(plr.userId) then
        local models = DATA:GetAsync(plr.userId)
        for _,v in pairs(models) do
            if plr.Tycoon.Value ~= "" then
                local baseplate = workspace:FindFirstChild(plr.Tycoon.Value).Base
                local createdModel = game.ReplicatedStorage.Objects:FindFirstChild(v[1]):Clone()
                local offset = Vector3.new(v[2][1],v[2][2],v[2][3])
                createdModel:SetPrimaryPartCFrame(CFrame.new(baseplate.Position + offset))
                print("Hello"..baseplate)
            end
        end
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    plr:WaitForDataReady()
    if plr.Tycoon.Value ~= "" then
        local tycoon = workspace:FindFirstChild(plr.Tycoon.Value)
        local baseplate = tycoon:FindFirstChild("Base") --set to tycoon baseplate
        local models = {}
        for _,v in pairs(tycoon:GetChildren()) do
            if v:IsA("Model") and v.PrimaryPart then
            local offset = v.PrimaryPart.Position - baseplate.Position
            table.insert(models,1,{v.Name,{offset.X,offset.Y,offset.Z}})
        end
    end
end 
--save models table
end)

Thanks!

0
Also i dont currently have a placement system made as i wont bother if i dont end up knowing how to save the thing :P MezzaBlockGuy 2 — 6y

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago

You can just iterate through the models in the base and record their name and offset from the baseplate’s position (or some other core part that every tycoon has). Then, you would iterate through the loaded table, create the models and offset them from the baseplate position to return them to the same place. You would have to give all models a PrimaryPart so that you can move it as one, and put a copy of every model in ServerStorage.

Recording model positions:

local tycoon = --set to tycoon model
local baseplate = tycoon:FindFirstChild("Baseplate") --set to tycoon baseplate
local models = {}
for _,v in pairs(tycoon:GetChildren()) do
    if v:IsA("Model") and v.PrimaryPart then
        local offset = v.PrimaryPart.Position - baseplate.Position
        table.insert(models,1,{v.Name,{offset.X,offset.Y,offset.Z}})
    end
end 
--save models table

Loading model positions:

local models = --loaded tycoon models
for _,v in pairs(models) do
    local createdModel = game.ServerStorage:FindFirstChild(v[1]):Clone()
    local offset = Vector3.new(v[2][1],v[2][2],v[2][3])
    createdModel:SetPrimaryPartCFrame(CFrame.new(baseplate.Position + offset))
end
Ad

Answer this question