I have made this save script which worked at first but when I added new models it completely broke it and I have no clue why. It is supposed to save my models that have been placed into a folder in the workspace called SaveFolder. It saves it fine I think but it might be a problem loading the models and placing back into the folder. By the way my base models are stored in replicated storage in a Models folder. If anyone knows how to fix this it would be appreciated. I get the error on line 67!
-- Define local players = game:GetService("Players"); local DataStoreService = game:GetService("DataStoreService"); local DataStore = DataStoreService:GetDataStore("TycoonSave"); local saveFolder = workspace.SaveFolder; local saveTable = {}; -- Save function (will explain more in the function) local function Save(plr) local key = "plr-"..plr.UserId; -- we need to have a key for each player local objects = saveFolder:GetChildren(); -- all the objects -- Loops through all the objects in the saveFolder for i, obj in pairs(objects) do if obj then -- Inserts the name and position of the model table.insert(saveTable, { ["Name"] = obj.Name; ["CFS"] = { -- Positions are a bit complex ["X"] = obj.PrimaryPart.CFrame.X; ["Y"] = obj.PrimaryPart.CFrame.Y; ["Z"] = obj.PrimaryPart.CFrame.Z; ["ROT"] = obj.PrimaryPart.Orientation.Y } }) -- This is simple though we can access this again by saying saveTable.CFS.AXIS end end local success, err = pcall(function() -- prevents script from stopping DataStore:SetAsync(key, saveTable); -- Saves the table end) -- If the saving failed if not success then warn("Failed to over-write data"..tostring(err)); return; end end -- Load function (will explain more in the function) local function Load(plr) local key = "plr-"..plr.UserId; -- we need the players key so we can access their data local savedTycoon; -- We will set this in a pcall() local success, err = pcall(function() savedTycoon = DataStore:GetAsync(key); -- reads the data saved end) -- if error if not success then warn("Failed to read data"..tostring(err)); return; end -- If there was no error and if there is data to be loaded if savedTycoon then wait(1); for i, obj in pairs(savedTycoon) do if obj then -- We can use the saved data to access the name: obj.Name will give us the name local savedModel = game.ReplicatedStorage.Models:FindFirstChild(obj.Name):Clone(); if savedModel then -- Positions the model savedModel:SetPrimaryPartCFrame(CFrame.new(obj.CFS.X, obj.CFS.Y, obj.CFS.Z) * CFrame.Angles(0, math.rad(obj.CFS.ROT), 0)); savedModel.PrimaryPart.Transparency = 1; for index, value in pairs(savedModel:GetDescendants()) do if value then if value:IsA("BasePart") then value.CanCollide = true value.Transparency = 0 end end end savedModel.PrimaryPart.Transparency = 1 savedModel.PrimaryPart.CanCollide = false; savedModel.Parent = workspace.SaveFolder; end end end else Save(plr) end end game.ReplicatedStorage.Events.ClearPlot.OnServerEvent:Connect(function() for i, v in pairs(workspace.SaveFolder:GetChildren()) do if v then v:Destroy(); wait(0, 1); end end end) -- Calls players.PlayerAdded:Connect(Load); players.PlayerRemoving:Connect(Save); game.ReplicatedStorage.Events.Save.OnServerEvent:Connect(Save);