I need help with saving builds system

HI, I am here because I was using a free module from a creator because I didn’t know how to make a building system like Bloxburg, and I was trying to make it save data.

I work but it’s all jumbled to gather, and it does not look like what I have built before and I tried running it though AI to fix it, but it got it even worse.

local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local wallDataStore = DataStoreService:GetDataStore("WallData11") -- Changed to WallData10

local playerPlots = {} -- Store player plot assignments

-- **Track Selected Plot for Each Player & Load and Move Walls**
game.ReplicatedStorage.Events.SetPlot.OnServerEvent:Connect(function(Player, Plot)
	playerPlots[Player.UserId] = Plot -- Save the chosen plot

	-- **Verify Plot Exists**
	if not Plot or not Plot.Objects or not Plot.Objects.Walls then
		warn("Plot, Objects, or Walls is nil for player:", Player.Name)
		return
	end

	local plotPosition = Plot.PrimaryPart.Position
	local plotSize = Plot.PrimaryPart.Size

	-- **Load Walls and Move Them to the Plot's Position**
	local successLoad, storedWallsJSON = pcall(function()
		return wallDataStore:GetAsync(Player.UserId) or "{}"
	end)

	if successLoad then
		local storedWalls = HttpService:JSONDecode(storedWallsJSON)

		for _, wallData in ipairs(storedWalls) do
			local newWall = Instance.new("Part")

			-- **Ensure Position and Size are Valid**
			if wallData.Position and wallData.Size then
				local storedPosition = Vector3.new(wallData.Position[1], wallData.Position[2], wallData.Position[3])
				local storedSize = Vector3.new(wallData.Size[1], wallData.Size[2], wallData.Size[3])

				-- **Adjust Wall Position Relative to the Plot**
				local newPosition = plotPosition + storedPosition - Vector3.new(plotSize.X / 2, 0, plotSize.Z / 2)
				newWall.Position = newPosition
				newWall.Size = storedSize
			else
				warn("Skipping wall due to missing Position or Size data.")
				continue
			end

			-- **Convert Material from stored string**
			if wallData.Material and Enum.Material[wallData.Material] then
				newWall.Material = Enum.Material[wallData.Material]
			else
				warn("Invalid material detected, defaulting to Plastic.")
				newWall.Material = Enum.Material.Plastic
			end

			newWall.Color = Color3.new(wallData.Color[1], wallData.Color[2], wallData.Color[3])
			newWall.Anchored = true
			newWall.Parent = Plot.Objects.Walls -- **Parenting the loaded wall properly**
		end
	else
		warn("Failed to load walls: " .. storedWallsJSON)
	end
end)

-- **Placing and Saving Walls**
game.ReplicatedStorage.Events.PlaceWall.OnServerEvent:Connect(function(Player, Point1, Point2)
	local Plot = playerPlots[Player.UserId] -- Retrieve the chosen plot

	-- **Verify Plot Exists**
	if not Plot or not Plot.Objects or not Plot.Objects.Walls then
		warn("Plot, Objects, or Walls is nil for player:", Player.Name)
		return
	end

	local Mag = (Point1 - Point2).magnitude

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {Player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(Point1, (Point1 - Point2).unit * Mag, raycastParams)
	local position = raycastResult and raycastResult.Position or Point2

	local Wall = Instance.new("Part", Plot.Objects.Walls) -- **Parenting to Plot.Objects.Walls**
	Wall.Anchored = true
	Wall.Transparency = 0
	Wall.Name = "Wall"
	Wall.TopSurface = Enum.SurfaceType.Smooth
	Wall.BottomSurface = Enum.SurfaceType.Smooth

	local Distance = (Point1 - position).magnitude
	Wall.Size = Vector3.new(0.5, 10, Mag + 0.75)
	Wall.CFrame = CFrame.new(Point1, position) * CFrame.new(0, 0, Mag / 2)

	-- **Ensure Position is Valid**
	if Wall.Position then
		print("Saving wall at:", Wall.Position)
	else
		warn("Wall Position is nil, skipping save.")
		return
	end

	-- **Save Wall Data**
	local wallData = {
		Position = {Wall.Position.X, Wall.Position.Y, Wall.Position.Z},
		Size = {Wall.Size.X, Wall.Size.Y, Wall.Size.Z},
		Material = Wall.Material.Name, -- Store enum name as a string
		Color = {Wall.Color.R, Wall.Color.G, Wall.Color.B}
	}

	local success, errorMessage = pcall(function()
		local existingWallsJSON = wallDataStore:GetAsync(Player.UserId) or "{}"
		local existingWalls = HttpService:JSONDecode(existingWallsJSON)

		table.insert(existingWalls, wallData)

		local updatedWallsJSON = HttpService:JSONEncode(existingWalls)
		wallDataStore:SetAsync(Player.UserId, updatedWallsJSON)
	end)

	if not success then
		warn("Failed to save wall: " .. errorMessage)
	end
end)

1 Like