Help with plot saving system

I have a plot saving system and it kinda works. Theres a problem tho the blocks are duplicating everytime I rejoin the game which means that there are a lot of parts in the same Position which is not very good for performance. I tried clearing the plot data table whenever the player leav or joins but when I joined the blocks were not appearing at all. Here is my script:

local Plots = game.Workspace:FindFirstChild("Plots")
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local PlotSave = DSS:GetDataStore("PlotSaveDSTest5")

local plotdata = {}

Players.PlayerRemoving:Connect(function(plr)
	--print("player removed")
	for i,v in pairs(Plots:GetDescendants()) do
		if v:IsA("StringValue") and v.Name == "Owner" then
			--print("There is a stringvalue Owner")
			--for e, plr in pairs(game.Players:GetPlayers()) do
				if plr.Name == v.Value then
					--print("The players name equals to the Owners value")
					local PlrsPlotModel = v.Parent
					for x, block in pairs(PlrsPlotModel:GetChildren()) do
						if block:FindFirstChild("Blockdestroy") and block:IsA("BasePart") then
							--print("There is a Blockdestroy value in that part")
							local blocksPositionInPlotX = block.CFrame:ToObjectSpace(v.Parent.Plot.CFrame).X
							local blocksPositionInPlotY = block.CFrame:ToObjectSpace(v.Parent.Plot.CFrame).Y
							local blocksPositionInPlotZ = block.CFrame:ToObjectSpace(v.Parent.Plot.CFrame).Z
							local blocksOrientationInPlotY = v.Parent.Plot.Orientation.Y - block.Orientation.Y
							--game.Players.PlayerRemoving:Connect(function(plr)
							--print("Player removed")
							local function roundNumber(num, numDecimalPlaces)
								return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
							end
							
							local blocksPositionInPlotXRound = roundNumber(blocksPositionInPlotX,1)
							local blocksPositionInPlotYRound = roundNumber(blocksPositionInPlotY,1)
							local blocksPositionInPlotZRound = roundNumber(blocksPositionInPlotZ,1)
							local blocksOrientationInPlotRound= roundNumber(blocksOrientationInPlotY,1)
							
							table.insert(plotdata,{
								["Name"] = block.Name,
								["X"] = blocksPositionInPlotXRound,
								["Y"] = blocksPositionInPlotYRound,
								["Z"] = blocksPositionInPlotZRound,
								["RY"] = blocksOrientationInPlotY
							})
							
							
							
							local succ, err = pcall(function()
								PlotSave:SetAsync(plr.UserId.."-Plot",plotdata)
								--print("Saved plot")
							end)
							if err then
								warn(err)
							end
							
							--plotdata = {}
							--end
						
						end
					end
				end
			--end
		end
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	local succ, err = pcall(function()
		plotdata = PlotSave:GetAsync(plr.UserId.."-Plot") or {}
	end)
	if err then
		warn(err)
	end
	print(plotdata)
	for i,v in pairs(plotdata) do
		--print(v)
		local block = game.ReplicatedStorage.Blocks:FindFirstChild(v.Name)
		for e,plot in pairs(game.Workspace.Plots:GetChildren()) do
			if plot.Owner.Value == plr.Name then
				if block.Name == v.Name then
					--print("Found plot")
					--plotdata = {}
					local newblock = block:Clone()
					newblock.Parent = plot
					newblock.CFrame = plot.Plot.CFrame * CFrame.new(v.X,v.Y* -1,v.Z)
					--newblock.Orientation = Vector3.new()
					plotdata = {}
				end
			end
		end
		
	end	
end)

Thank you for reading this :slight_smile:

1 Like

Do u copy this exactly from youtube or u modify this yourself

I modified this myself. I didnt find a tutorial which would fit.