Data Compression for OOP Datastore

So, basically, I have an OOP datastore system script here. But, there’s one problem, sometimes players can have hundreds of parts, and almost every time I leave the game with like two hundred parts in my plot, roblox studio crashes because of the heavy data size. I tried adding an auto saving system that saves every couple of seconds. I added in a math.random so that the DataStore does not get filled up with requests at the same amount of seconds every time. So, the main question is, how would I compress the player’s data? I know that Welcome to Bloxburg has an auto save for the player’s plot, so should I compress the data or just auto save it?

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("OOPData")

local keyPrefix = "Player: "
local parts

game.Players.PlayerAdded:Connect(function(player)
	local newFolder = Instance.new("Folder")
	newFolder.Name = player.Name
	newFolder.Parent = game.Workspace
	
	parts = newFolder
end)


local function save(player: Player)
	local key = keyPrefix..tostring(player.UserId)
	local data = {}
	
	for i, obj: BasePart in ipairs(parts:GetChildren()) do
		table.insert(data, {
			obj.Name,
			obj.CanCollide,
			obj.CFrame.Position.X,
			obj.CFrame.Position.Y,
			obj.CFrame.Position.Z,
			obj.Orientation.X,
			obj.Orientation.Y,
			obj.Orientation.Z,
			tostring(obj.BrickColor),
			
			--Size
			tonumber(obj.Size.X),
			tonumber(obj.Size.Y),
			tonumber(obj.Size.Z),
			
			--Anchored
			obj.Anchored
			
		})
	end
	
	local success, err
	
	repeat 
		success, err = pcall(function()
			dataStore:UpdateAsync(key, function()
				return data
			end)
			
		end)
		task.wait()
	until success
	
	if not success then
		warn("Failed to save data")
		player:Kick("Failed to save data")
	end
end

local function load(player: Player)
	local key = keyPrefix..tostring(player.UserId)
	local success, err
	
	local data
	
	repeat 
		success, err = pcall(function()
			data = dataStore:GetAsync(key)
		end)
	until success or not players:FindFirstChild(player.Name)
	
	if not data then return end
	
	if success then
		for i, obj in ipairs(data) do
			local newPart = Instance.new("Part")
			newPart.Name = obj[1]
			newPart.CanCollide = obj[2]
			newPart.CFrame = CFrame.new(obj[3], obj[4], obj[5])
			newPart.CFrame *= CFrame.Angles(obj[6]*math.pi/180, obj[7]*math.pi/180, obj[8]*math.pi/180)
			newPart.BrickColor = BrickColor.new(obj[9])
			newPart.Size = Vector3.new(obj[10], obj[11], obj[12])
			newPart.Anchored = obj[13]
			
			newPart.Parent = game.Workspace:FindFirstChild(player.Name)
		end
	else
		warn("Failed to learn data")
	end
end


players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)

game.Players.PlayerAdded:Connect(function(player)
	local randomTimes = {20, 10, 9, 10}
	while true do
		wait(randomTimes[math.random(1, #randomTimes)])
		print("Data Auto Saved")
		save(player)
	end
end)
game:BindToClose(function()
	for i, plr: Player in ipairs(players:GetPlayers()) do
		save(plr)
		wait(60)
	end
end)

This really depends on your building system, because the Name might not be important, and if most objects are anchored, then you can only put anchored to false, but not true, same goes with canCollide. On top of this, if you have a set list of parts, that have a set brick color and size, you can put the id instead of the size and color.

Again, this all depends on your building system.

Thank you, I’ll try that out right now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.