Here is the code, I need to know how to save an object's material.

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataBaseV1.5")

local plot

local function GetPlot(plr)
	for i, plt in pairs(workspace.Plots:GetChildren()) do
		if plt then 
			if plt.Plot.Owner.Value == plr then
				plot = workspace.Plots:FindFirstChild(plt.Name)	
				
				break
			end
		end
	end
end

local function Save(plr)
	local key = "plr-"..plr.UserId
	
	local save = {}
	
	for i, obj in pairs(plot.PlacedObjects:GetChildren()) do
		if obj then
			table.insert(save, {
				["Name"] = obj.Name,
					
				["CFS"] = {
					["X"] = plot.Plot.CFrame:ToObjectSpace(CFrame.new(obj.PrimaryPart.CFrame.p)).X;
					["Y"] = plot.Plot.CFrame:ToObjectSpace(CFrame.new(obj.PrimaryPart.CFrame.p)).Y;
					["Z"] = plot.Plot.CFrame:ToObjectSpace(CFrame.new(obj.PrimaryPart.CFrame.p)).Z;
						
					["R"] = obj.PrimaryPart.Orientation.Y
				},
					
				["Color"] = {
					["R"] = obj.Paintable.PrimaryPart.Color.R,
					["G"] = obj.Paintable.PrimaryPart.Color.G,
					["B"] = obj.Paintable.PrimaryPart.Color.B,
				},
				
				["Material"] = obj.Paintable.PrimaryPart.MaterialValue
			})
		end
	end
	
	local success, err = pcall(function()
		DataStore:SetAsync(key, save)
	end)
	
	if not success then
		warn("Failed to over-read data: "..tostring(err))
		return
	end
end

local function Load(plr)
	wait(1)
	
	GetPlot(plr)
	
	--DataStore Below	
	
	local key = "plr-"..plr.UserId
	
	local savedData
		
	local success, err = pcall(function()
		savedData = DataStore:GetAsync(key)
	end)
	
	if not success then
		warn("Failed to read data: "..tostring(err))
		return
	end
	
	if savedData then
		for i, data in pairs(savedData) do
			if data then
				local serializeModel = game.ReplicatedStorage.Models:FindFirstChild(data.Name):Clone()
				
				if serializeModel then
					serializeModel.PrimaryPart.Transparency = 1
					
					serializeModel:SetPrimaryPartCFrame(plot.Plot.CFrame * CFrame.new(data.CFS.X, data.CFS.Y, data.CFS.Z) * CFrame.Angles(0, math.rad(data.CFS.R), 0))	
					
					for _, o in pairs(serializeModel.Paintable:GetChildren()) do
						if o then
							o.Color = Color3.new(data.Color.R, data.Color.G, data.Color.B)
							
						end
					end
					
					serializeModel.Parent = plot.PlacedObjects
				end
			end
		end
	else
		Save(plr)
	end
end

local function UnloadTycoonData(plr)
	GetPlot(plr)
	
	Save(plr)
	
	for i, owners in pairs(plot.Plot:GetChildren()) do
		if owners then
			if owners:IsA("ObjectValue") and owners:IsA("StringValue") then
				owners.Value = nil
			end
		end
	end
	
	for i, objs in pairs(plot.PlacedObjects:GetChildren()) do
		if objs then
			objs:Destroy()
		end
	end
end

game.ReplicatedStorage.Events.Serialize.OnServerEvent:Connect(Save)

players.PlayerAdded:Connect(Load)
players.PlayerRemoving:Connect(UnloadTycoonData)

Thanks for the help!

6 comments

1

why dont u just turn the material into a string and save the string then load it as material xd

0

How?

0

I also used an int value (does that work?)

0

this code clearly doesnt belong to you..?

0

yep - borrowed it from zblox

0

nevermind - I figured it out, thanks for the help!