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);

4 comments

0

What type of script are you using? Are the models free models? You could be trying to index a nongroupable object.

0

Also, folders and models shouldn't be placed in ReplicatedStorage unless you fully know what you're doing.

0

The models are all custom made they individually groupped and placing inside a folder. Now the parts are link to my placement script and place down fine and are added to the workspace folder just fine. It just doesn't load after you save and rejoin. The only reason that it is placed in a folder in replicatedstorage is so I can be stored and placed into workspace folder.

0

If line 67 is an error then it would be "attemped to index local savedModel (a boolean value)" because FindFirstChild can return the child OR false (if it doesn't exist). Check if the model exists before you try and clone it.