Hey,
I have made a plot/block saving system which saves the blocks within a plot and loads them when the player joins. There are some issues with the system.
- Blocks turning 180 degrees on the y axis
- Not every block being loaded or saved correctly
before rejoining:
after rejoining:
- Block duplication (2 or more blocks being in the same exact position)
Here is my Code:
local Plots = game.Workspace:FindFirstChild("Plots")
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local PlotSave = DSS:GetDataStore("PlotSaveDSTest9")
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
--plotdata = {}
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,3)
local blocksPositionInPlotYRound = roundNumber(blocksPositionInPlotY,3)
local blocksPositionInPlotZRound = roundNumber(blocksPositionInPlotZ,3)
local blocksOrientationInPlotRound= roundNumber(blocksOrientationInPlotY,3)
table.insert(plotdata,{
["Name"] = block.Name,
["X"] = blocksPositionInPlotX,
["Y"] = blocksPositionInPlotY,
["Z"] = blocksPositionInPlotZ,
["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(0,v.RY,0)
--plotdata = {}
end
end
end
end
end)
If you have any questions please ask me. I thank you for taking your time and reading through this post .