HI, My script below is a placement/save script however for some reason everytime i save/restore a plot it only restores the last sessions data, so any other sessions are lost. I don’t know what is going on so any help would be great.
-- Function to save player blocks
local function savePlayerBlocks(player)
if not player then return end
local userId = player.UserId
if not userId then return end
local blocks = playerBlocks[userId]
local plotIdentifier = playerPlots[userId]
if not plotIdentifier then
warn("Plot identifier not found for userId: " .. tostring(userId))
return
end
local plotGroup = game.Workspace.Plots:FindFirstChild(plotIdentifier)
if not plotGroup then
warn("Plot group not found for identifier: " .. tostring(plotIdentifier))
return
end
-- Debugging step to list children of plotGroup to understand structure
print("Children of plotGroup:", plotGroup:GetChildren())
-- Try to locate the PrimaryPart within the plot group
local plot = plotGroup:FindFirstChildWhichIsA("BasePart")
if not plot then
warn("Primary part not found in plot group for identifier: " .. tostring(plotIdentifier))
return
end
print("Found plot primary part:", plot.Name)
local plotPosition = plot.Position -- Use PrimaryPart for accurate positioning
local blockData = {}
if type(blocks) == "table" then
for _, block in pairs(blocks) do
local position
if block:IsA("Part") then
position = block.Position - plotPosition
elseif block:IsA("Model") and block.PrimaryPart then
position = block.PrimaryPart.Position - plotPosition
else
position = Vector3.new(0, 0, 0) -- Default or fallback value
end
table.insert(blockData, {Name = block.Name, Position = {position.X, position.Y, position.Z}})
end
else
warn("Blocks for userId " .. tostring(userId) .. " are not in the expected format.")
return
end
local jsonBlockData = HttpService:JSONEncode(blockData)
local success, errorMessage = pcall(function()
myDataStore:SetAsync(userId, jsonBlockData)
end)
if not success then
warn("Failed to save player blocks: " .. tostring(errorMessage))
end
end
-- Restore blocks using json
local function restoreBlocks(player, plotName)
if not player then return end
local userId = player.UserId
clearPlayerPlot(userId)
local plotIdentifier = plotName or playerPlots[userId]
playerPlots[userId] = plotIdentifier
local plotGroup = game.Workspace.Plots:FindFirstChild(plotIdentifier)
if plotGroup then
-- Look for the Part named as the plotIdentifier
local plotPart = plotGroup:FindFirstChild(plotIdentifier)
if not plotPart or not plotPart:IsA("Part") then
warn("Primary part not found in plot group for identifier: " .. tostring(plotIdentifier))
return
end
local plotPosition = plotPart.Position
local success, jsonBlockData = pcall(function()
return myDataStore:GetAsync(userId)
end)
if success and jsonBlockData then
local blockData
local decodeSuccess, errorMessage = pcall(function()
blockData = HttpService:JSONDecode(jsonBlockData)
end)
if decodeSuccess then
for _, data in pairs(blockData) do
local blockToPlace = findBlockInGroups(ReplicatedStorage.Builder, data.Name)
if blockToPlace then
blockToPlace = blockToPlace:Clone()
local relativePosition = Vector3.new(unpack(data.Position))
if blockToPlace:IsA("Model") and blockToPlace.PrimaryPart then
blockToPlace:SetPrimaryPartCFrame(CFrame.new(plotPosition + relativePosition))
elseif blockToPlace:IsA("Part") then
blockToPlace.Position = plotPosition + relativePosition
end
blockToPlace.Parent = plotGroup
-- Handle anchoring correctly for both Model and Part
if blockToPlace:IsA("Model") then
for _, part in pairs(blockToPlace:GetDescendants()) do
if part:IsA("BasePart") then
part.Anchored = true
end
end
elseif blockToPlace:IsA("BasePart") then
blockToPlace.Anchored = true
end
end
end
else
warn("Failed to decode block data for player: " .. player.Name)
end
else
warn("Failed to retrieve block data for player: " .. player.Name)
end
else
warn("Plot not found for restoration: " .. tostring(plotIdentifier))
end
end
-- Listen for the restore event
restoreEvent.OnServerEvent:Connect(restoreBlocks)
-- Listen for when a player leaves to save their blocks
Players.PlayerRemoving:Connect(savePlayerBlocks)