Problem with my placement script how can i fix it?

  1. I want to display items within a gui which users can click on it then deducts the money from their Money in leaderstats and then closes the GUi and allows the users to place these items anywhere on their plots.

  2. The Blocks do not show in the gui, they dont show for the plot. see video.
    robloxapp-20230922-1524268.wmv (435.1 KB)

here is a couple of screen shots to show where things are:
Screenshot 2023-09-22 152711
Screenshot 2023-09-22 152637
PlacementGUI script:

-- Client-Side Script: PlacementGuiScript
print("Script started")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local user = Players.LocalPlayer
local placementEvent = ReplicatedStorage:WaitForChild("PlacementEvent")
local getPlotFunction = ReplicatedStorage:WaitForChild("GetPlotFunction")  -- Add this line

local leaderstats = user:WaitForChild("leaderstats")  -- Wait for leaderstats to be available
local moneyValue = leaderstats:WaitForChild("Money")  -- Wait for Money to be available

local getSetMoney = ReplicatedStorage:WaitForChild("GetSetMoney")

-- Function to handle leaderboard setup
local function handleLeaderboardSetup()
	print("Leaderboard already set. Nothing to do here.")
end

local leaderboardSetupEvent = ReplicatedStorage:WaitForChild("LeaderboardSetupEvent", 5)
if leaderboardSetupEvent then
	leaderboardSetupEvent.OnClientEvent:Connect(handleLeaderboardSetup)
else
	print("Failed to find leaderboardSetupEvent")
end

local buildConfigs = script.Parent
local blocksShowcase = buildConfigs:WaitForChild("BlocksShowcase")
local scrollFrame = blocksShowcase:WaitForChild("ScrollFrame")
local openGuiButton = buildConfigs:WaitForChild("OpenGui")

local closeButton = blocksShowcase:FindFirstChild("Close")

local placementEvent = ReplicatedStorage:WaitForChild("PlacementEvent")

local selectedBlock = nil

-- Function to place a block on a plot
local function placeBlock()
	print("Attempting to place block...")
	local currentMoney = getSetMoney:InvokeServer("get")
	local plotName = getPlotFunction:InvokeServer()  -- Add this line
	if selectedBlock and currentMoney and plotName then  -- Modify this line
		local cost = selectedBlock:FindFirstChild("Cost")
		if cost and currentMoney >= cost.Value then
			print("Placing block...")
			getSetMoney:InvokeServer("set", currentMoney - cost.Value)
			placementEvent:FireServer(selectedBlock.Name, plotName)  -- Modify this line
		else
			print("Not enough money or invalid block")
		end
	else
		print("No block selected, money not found or plot not owned")
	end
end
-- Function to toggle BlocksShowcase GUI visibility
local function toggleGui()
	blocksShowcase.Visible = not blocksShowcase.Visible
end

-- Function to close BlocksShowcase GUI
local function closeGui()
	blocksShowcase.Visible = false
end

-- Populating blocks
for _, category in pairs({"Builds", "Decoration", "Interior"}) do
	local categoryFolder = ReplicatedStorage:FindFirstChild("Builder"):FindFirstChild(category)
	if categoryFolder then
		for _, block in pairs(categoryFolder:GetChildren()) do
			if block:IsA("Model") then
				local cost = block:FindFirstChild("Cost")
				if cost then
					local blockButton = Instance.new("TextButton")
					blockButton.Size = UDim2.new(0, 128, 0, 128)
					blockButton.BackgroundColor3 = Color3.new(1, 1, 1)
					blockButton.Text = ""
					blockButton.Parent = scrollFrame

					local viewport = Instance.new("ViewportFrame")
					viewport.Parent = blockButton

					local camera = Instance.new("Camera")
					camera.Parent = viewport
					viewport.CurrentCamera = camera
					camera.CameraType = Enum.CameraType.Scriptable
					if block.PrimaryPart then
						camera.CFrame = CFrame.new(block.PrimaryPart.Position + Vector3.new(5, 5, 5), block.PrimaryPart.Position)
					end

					local costLabel = Instance.new("TextLabel")
					costLabel.Size = UDim2.new(1, 0, 0, 20)
					costLabel.Position = UDim2.new(0, 0, 1, -20)
					costLabel.Text = "Cost: " .. cost.Value
					costLabel.Parent = blockButton

					local displayBlock = block:Clone()  -- Cloning the block
					displayBlock.Parent = viewport      -- Setting the clone as the child of viewport

					blockButton.MouseButton1Click:Connect(function()
						selectedBlock = block
						placeBlock()
						blocksShowcase.Visible = false  -- Close the GUI after placing the block
					end)
				end
			end
		end
	end
end


-- Connect the OpenGui button to toggle GUI
if openGuiButton then
	openGuiButton.MouseButton1Click:Connect(toggleGui)
end

-- Connect the Close button to close GUI
if closeButton then
	closeButton.MouseButton1Click:Connect(closeGui)
end

-- Optionally, you can use UserInputService for touch support
if UserInputService then
	UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch and input.UserInputState == Enum.UserInputState.Begin then
			local touchPos = input.Position
			local guiPos = openGuiButton.AbsolutePosition
			local guiSize = openGuiButton.AbsoluteSize

			if touchPos.x >= guiPos.x and touchPos.x <= guiPos.x + guiSize.x and touchPos.y >= guiPos.y and touchPos.y <= guiPos.y + guiSize.y then
				toggleGui()
			end
		end
	end)
else
	print("Failed to find UserInputService.")
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local restoreEvent = ReplicatedStorage:WaitForChild("RestoreEvent")

local restoreButton = script.Parent:FindFirstChild("RestoreButton")  -- Replace with the actual path to your Restore button

if restoreButton then
	restoreButton.MouseButton1Click:Connect(function()
		restoreEvent:FireServer()
	end)
end

PlacementSystem in ServerScriptService

-- Server-Side Script: PlacementSystem

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("PlayerBlocksDataStore")

local restoreEvent = Instance.new("RemoteEvent")
restoreEvent.Name = "RestoreEvent"
restoreEvent.Parent = ReplicatedStorage

local placementEvent = Instance.new("RemoteEvent")
placementEvent.Name = "PlacementEvent"
placementEvent.Parent = ReplicatedStorage

-- Table to store placed blocks for each player
local playerBlocks = {}

local function onBlockPlaced(player, blockName, plotIdentifier)
	print("Received request to place block: " .. blockName)

	local builder = ReplicatedStorage:FindFirstChild("Builder")
	if builder then
		local blockFound = builder:FindFirstChild(blockName)
		if blockFound then
			local blockToPlace = blockFound:Clone()
			local plot = game.Workspace:FindFirstChild(plotIdentifier)
			if plot then
				blockToPlace.Position = plot.Position + Vector3.new(0, 10, 0)
				blockToPlace.Parent = plot
				blockToPlace.Anchored = true
				print("Block placed successfully")

				if not playerBlocks[player.UserId] then
					playerBlocks[player.UserId] = {}
				end
				table.insert(playerBlocks[player.UserId], blockToPlace)
			else
				print("Invalid plot identifier")
			end
		else
			print("Block not found: " .. blockName)
		end
	else
		print("Builder folder not found in ReplicatedStorage.")
	end
end

-- Connect the function to handle the event
placementEvent.OnServerEvent:Connect(onBlockPlaced)

-- Function to save player blocks
local function savePlayerBlocks(player)
	local userId = player.UserId
	local blocks = playerBlocks[userId]

	if blocks then
		local blockData = {}
		for _, block in pairs(blocks) do
			table.insert(blockData, {Name = block.Name, Position = block.Position})
		end

		local success, errorMessage = pcall(function()
			myDataStore:SetAsync(userId, blockData)
		end)

		if not success then
			print("Failed to save data: ", errorMessage)
		end
	end
end

-- Function to restore player blocks
local function restoreBlocks(player)
	local userId = player.UserId

	local success, blockData = pcall(function()
		return myDataStore:GetAsync(userId)
	end)

	if success and blockData then
		for _, data in pairs(blockData) do
			local blockToPlace = ReplicatedStorage:FindFirstChild("Builder"):FindFirstChild(data.Name):Clone()
			if blockToPlace then
				blockToPlace.Position = data.Position
				blockToPlace.Parent = game.Workspace:FindFirstChild(player.Name .. "_Plot")
				blockToPlace.Anchored = true
			end
		end
	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)

The output i’m getting is:
15:24:35.595 Attempting to place block… - Client - PlacementGuiScript:41
15:24:35.758 Placing block… - Client - PlacementGuiScript:47
15:24:35.839 Received request to place block: Step - Server - PlacementSystem:21
15:24:35.839 Block not found: Step - Server - PlacementSystem:43

any help would be great.

2 Likes

Blind guess. If this works it’s a miracle.

Client-Side Script: PlacementGuiScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local user = Players.LocalPlayer
local placementEvent = ReplicatedStorage:WaitForChild("PlacementEvent")
local getPlotFunction = ReplicatedStorage:WaitForChild("GetPlotFunction")

local leaderstats = user:WaitForChild("leaderstats")
local moneyValue = leaderstats:WaitForChild("Money")
local getSetMoney = ReplicatedStorage:WaitForChild("GetSetMoney")

local buildConfigs = script.Parent
local blocksShowcase = buildConfigs:WaitForChild("BlocksShowcase")
local scrollFrame = blocksShowcase:WaitForChild("ScrollFrame")
local openGuiButton = buildConfigs:WaitForChild("OpenGui")
local closeButton = blocksShowcase:FindFirstChild("Close")
local selectedBlock = nil

local function placeBlock()
	local currentMoney = getSetMoney:InvokeServer("get")
	local plotName = getPlotFunction:InvokeServer()

	if selectedBlock and currentMoney and plotName then
		local cost = selectedBlock:FindFirstChild("Cost")

		if cost and currentMoney >= cost.Value then
			getSetMoney:InvokeServer("set", currentMoney - cost.Value)
			placementEvent:FireServer(selectedBlock.Name, plotName)
		end
	end
end

local function toggleGui()
	blocksShowcase.Visible = not blocksShowcase.Visible
end

local function closeGui()
	blocksShowcase.Visible = false
end

for _, category in pairs({"Builds", "Decoration", "Interior"}) do
	local categoryFolder = ReplicatedStorage:FindFirstChild("Builder"):FindFirstChild(category)

	if categoryFolder then
		for _, block in pairs(categoryFolder:GetChildren()) do
			if block:IsA("Model") then
				local cost = block:FindFirstChild("Cost")

				if cost then
					local blockButton = Instance.new("TextButton")
					blockButton.Size = UDim2.new(0, 128, 0, 128)
					blockButton.BackgroundColor3 = Color3.new(1, 1, 1)
					blockButton.Text = ""
					blockButton.Parent = scrollFrame

					local viewport = Instance.new("ViewportFrame")
					viewport.Parent = blockButton

					local camera = Instance.new("Camera")
					camera.Parent = viewport
					viewport.CurrentCamera = camera
					camera.CameraType = Enum.CameraType.Scriptable

					if block.PrimaryPart then
						camera.CFrame = CFrame.new(block.PrimaryPart.Position + Vector3.new(5, 5, 5), block.PrimaryPart.Position)
					end

					local costLabel = Instance.new("TextLabel")
					costLabel.Size = UDim2.new(1, 0, 0, 20)
					costLabel.Position = UDim2.new(0, 0, 1, -20)
					costLabel.Text = "Cost: " .. cost.Value
					costLabel.Parent = blockButton

					local displayBlock = block:Clone()
					displayBlock.Parent = viewport

					blockButton.MouseButton1Click:Connect(function()
						selectedBlock = block
						placeBlock()
						blocksShowcase.Visible = false
					end)
				end
			end
		end
	end
end

if openGuiButton then
	openGuiButton.MouseButton1Click:Connect(toggleGui)
end

if closeButton then
	closeButton.MouseButton1Click:Connect(closeGui)
end

if UserInputService then
	UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch and input.UserInputState == Enum.UserInputState.Begin then
			local touchPos = input.Position
			local guiPos = openGuiButton.AbsolutePosition
			local guiSize = openGuiButton.AbsoluteSize

			if touchPos.x >= guiPos.x and touchPos.x <= guiPos.x + guiSize.x and touchPos.y >= guiPos.y and touchPos.y <= guiPos.y + guiSize.y then
				toggleGui()
			end
		end
	end)
end
Server-Side Script: PlacementSystem
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("PlayerBlocksDataStore")

local restoreEvent = Instance.new("RemoteEvent")
restoreEvent.Name = "RestoreEvent"
restoreEvent.Parent = ReplicatedStorage

local placementEvent = Instance.new("RemoteEvent")
placementEvent.Name = "PlacementEvent"
placementEvent.Parent = ReplicatedStorage

local playerBlocks = {}

local function onBlockPlaced(player, blockName, plotIdentifier)
	local builder = ReplicatedStorage:FindFirstChild("Builder")

	if builder then
		local blockFound = builder:FindFirstChild(blockName)

		if blockFound then
			local blockToPlace = blockFound:Clone()
			local plot = game.Workspace:FindFirstChild(plotIdentifier)

			if plot then
				blockToPlace.Position = plot.Position + Vector3.new(0, 10, 0)
				blockToPlace.Parent = plot
				blockToPlace.Anchored = true

				if not playerBlocks[player.UserId] then
					playerBlocks[player.UserId] = {}
				end
				table.insert(playerBlocks[player.UserId], blockToPlace)
			end
		end
	end
end

placementEvent.OnServerEvent:Connect(onBlockPlaced)

local function savePlayerBlocks(player)
	local userId = player.UserId
	local blocks = playerBlocks[userId]

	if blocks then
		local blockData = {}
		for _, block in pairs(blocks) do
			table.insert(blockData, {Name = block.Name, Position = block.Position})
		end

		local success, errorMessage = pcall(function()
			myDataStore:SetAsync(userId, blockData)
		end)

		if not success then
			print("Failed to save data: ", errorMessage)
		end
	end
end

local function restoreBlocks(player)
	local userId = player.UserId

	local success, blockData = pcall(function()
		return myDataStore:GetAsync(userId)
	end)

	if success and blockData then
		for _, data in pairs(blockData) do
			local blockToPlace = ReplicatedStorage:FindFirstChild("Builder"):FindFirstChild(data.Name):Clone()
			if blockToPlace then
				blockToPlace.Position = data.Position
				blockToPlace.Parent = game.Workspace:FindFirstChild(player.Name .. "_Plot")
				blockToPlace.Anchored = true
			end
		end
	end
end

restoreEvent.OnServerEvent:Connect(restoreBlocks)

Players.PlayerRemoving:Connect(savePlayerBlocks)

Thanks, but that broke the leaderboard. and some other functions.

This is the fix.
PlacementGuiScript:

-- Client-Side Script: PlacementGuiScript
print("Script started")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local user = Players.LocalPlayer
local placementEvent = ReplicatedStorage:WaitForChild("PlacementEvent")
local getPlotFunction = ReplicatedStorage:WaitForChild("GetPlotFunction")  -- Add this line

local leaderstats = user:WaitForChild("leaderstats")  -- Wait for leaderstats to be available
local moneyValue = leaderstats:WaitForChild("Money")  -- Wait for Money to be available

local getSetMoney = ReplicatedStorage:WaitForChild("GetSetMoney")

-- Function to handle leaderboard setup
local function handleLeaderboardSetup()
	print("Leaderboard already set. Nothing to do here.")
end

local leaderboardSetupEvent = ReplicatedStorage:WaitForChild("LeaderboardSetupEvent", 5)
if leaderboardSetupEvent then
	leaderboardSetupEvent.OnClientEvent:Connect(handleLeaderboardSetup)
else
	print("Failed to find leaderboardSetupEvent")
end

local buildConfigs = script.Parent
local blocksShowcase = buildConfigs:WaitForChild("BlocksShowcase")
local scrollFrame = blocksShowcase:WaitForChild("ScrollFrame")
local openGuiButton = buildConfigs:WaitForChild("OpenGui")

local closeButton = blocksShowcase:FindFirstChild("Close")

local placementEvent = ReplicatedStorage:WaitForChild("PlacementEvent")

local selectedBlock = nil
local previewBlock = nil

-- Function to place a block on a plot
local function placeBlock()
	print("Attempting to place block...")
	local currentMoney = getSetMoney:InvokeServer("get")
	local plotName = getPlotFunction:InvokeServer()
	if selectedBlock and currentMoney and plotName then
		local cost = selectedBlock:FindFirstChild("Cost")
		if cost and currentMoney >= cost.Value then
			print("Attempting to preview block placement...")
			placementEvent:FireServer("preview", selectedBlock.Name, plotName)
			-- Wait for user confirmation here. This is a placeholder; replace it with your confirmation logic.
			local userConfirmed = true  
			if userConfirmed then
				print("Placing block...")
				getSetMoney:InvokeServer("set", currentMoney - cost.Value)
				placementEvent:FireServer("place", selectedBlock.Name, plotName)
			else
				print("Block placement canceled.")
			end
		else
			print("Not enough money or invalid block")
		end
	else
		print("No block selected, money not found or plot not owned")
	end
end
-- Function to toggle BlocksShowcase GUI visibility
local function toggleGui()
	blocksShowcase.Visible = not blocksShowcase.Visible
end

-- Function to close BlocksShowcase GUI
local function closeGui()
	blocksShowcase.Visible = false
end

-- Create and configure UIGridLayout
local gridLayout = Instance.new("UIGridLayout")
gridLayout.CellPadding = UDim2.new(0, 10, 0, 10)
gridLayout.CellSize = UDim2.new(0, 128, 0, 128)
gridLayout.FillDirection = Enum.FillDirection.Horizontal
gridLayout.SortOrder = Enum.SortOrder.LayoutOrder
gridLayout.Parent = scrollFrame

-- Populating blocks
print("Starting to populate blocks")
for _, category in pairs({"Builds", "Decoration", "Interior"}) do
	local categoryFolder = ReplicatedStorage:FindFirstChild("Builder"):FindFirstChild(category)
	if categoryFolder then
		print("Category found: ", category)
		for _, block in pairs(categoryFolder:GetChildren()) do
			if block:IsA("Model") then
				local cost = block:FindFirstChild("Cost")
				if cost then
					print("Creating button for block: ", block.Name)
					local blockButton = Instance.new("TextButton")
					blockButton.Size = UDim2.new(0, 128, 0, 128)
					blockButton.BackgroundColor3 = Color3.fromRGB(236,236,236)  -- Background color of the button
					blockButton.Text = ""
					blockButton.TextColor3 = Color3.fromRGB(0, 0, 0)  -- Text color of the button (not applicable here since Text is empty)
					blockButton.Parent = scrollFrame

					local viewport = Instance.new("ViewportFrame")
					viewport.Size = UDim2.new(1, 0, 1, 0)  -- Fill the entire TextButton
					viewport.Parent = blockButton
					viewport.BackgroundColor3 = Color3.fromRGB(236,236,236)

					local camera = Instance.new("Camera")
					camera.Parent = viewport
					viewport.CurrentCamera = camera
					camera.CameraType = Enum.CameraType.Scriptable
					if block.PrimaryPart then
						camera.CFrame = CFrame.new(block.PrimaryPart.Position + Vector3.new(5, 5, 5), block.PrimaryPart.Position)
					end
					
					local rotationCFrame = CFrame.Angles(0, math.rad(10), 0)  -- Rotating 10 degrees around Y-axis

					if block.PrimaryPart then
						local targetPosition = block.PrimaryPart.Position
						local originalPosition = targetPosition + Vector3.new(5, 5, 5)
						local originalCFrame = CFrame.new(originalPosition, targetPosition)

						-- Apply rotation to the original CFrame and re-adjust the position
						local rotatedCFrame = originalCFrame * rotationCFrame
						local newPosition = rotatedCFrame.Position
						camera.CFrame = CFrame.new(newPosition, targetPosition)
					end

					local costLabel = Instance.new("TextLabel")
					costLabel.Size = UDim2.new(1, 0, 0, 20)
					costLabel.Position = UDim2.new(0, 0, 1, -20)
					costLabel.Text = " " .. block.Name .. " | " .. cost.Value
					costLabel.TextColor3 = Color3.fromRGB(255, 255, 255)  -- Text color of the label
					costLabel.BackgroundColor3 = Color3.fromRGB(85, 85, 0)  -- Background color of the label
					costLabel.Parent = blockButton

					local displayBlock = block:Clone()
					displayBlock.Parent = viewport

					-- Add rotation here
					local rotationCFrame = CFrame.Angles(0, math.rad(210), 0)  -- Rotating 10 degrees around Y-axis
					displayBlock:SetPrimaryPartCFrame(displayBlock.PrimaryPart.CFrame * rotationCFrame)

					blockButton.MouseButton1Click:Connect(function()
						selectedBlock = block
						placeBlock()
						blocksShowcase.Visible = false  -- Close the GUI after placing the block
					end)
				else
					print("Cost not found for block: ", block.Name)
				end
			else
				print("Not a model: ", block.Name)
			end
		end
	else
		print("Category not found: ", category)
	end
end
print("Finished populating blocks")


-- Connect the OpenGui button to toggle GUI
if openGuiButton then
	openGuiButton.MouseButton1Click:Connect(toggleGui)
end

-- Connect the Close button to close GUI
if closeButton then
	closeButton.MouseButton1Click:Connect(closeGui)
end

-- Optionally, you can use UserInputService for touch support
if UserInputService then
	UserInputService.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch and input.UserInputState == Enum.UserInputState.Begin then
			local touchPos = input.Position
			local guiPos = openGuiButton.AbsolutePosition
			local guiSize = openGuiButton.AbsoluteSize

			if touchPos.x >= guiPos.x and touchPos.x <= guiPos.x + guiSize.x and touchPos.y >= guiPos.y and touchPos.y <= guiPos.y + guiSize.y then
				toggleGui()
			end
		end
	end)
else
	print("Failed to find UserInputService.")
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local restoreEvent = ReplicatedStorage:WaitForChild("RestoreEvent")

local restoreButton = script.Parent:FindFirstChild("RestoreButton")  -- Replace with the actual path to your Restore button

if restoreButton then
	restoreButton.MouseButton1Click:Connect(function()
		restoreEvent:FireServer()
	end)
end

PlacementSystem script:


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("PlayerBlocksDataStore")

local restoreEvent = Instance.new("RemoteEvent")
restoreEvent.Name = "RestoreEvent"
restoreEvent.Parent = ReplicatedStorage

local placementEvent = Instance.new("RemoteEvent")
placementEvent.Name = "PlacementEvent"
placementEvent.Parent = ReplicatedStorage

-- Table to store placed blocks for each player
local playerBlocks = {}

local function onBlockPlaced(player, action, blockName, plotIdentifier)
	print("Received request to place block: " .. blockName)

	local function findBlockInGroups(group)
		for _, child in pairs(group:GetChildren()) do
			if child.Name == blockName then
				return child
			elseif child:IsA("Folder") or child:IsA("Model") then
				local foundBlock = findBlockInGroups(child)
				if foundBlock then return foundBlock end
			end
		end
		return nil
	end

	local builder = ReplicatedStorage:FindFirstChild("Builder")
	local blockFound = findBlockInGroups(builder)

	if action == "preview" then
		print("Previewing block: " .. blockName)
	elseif action == "place" then
		if blockFound then
			local blockToPlace = blockFound:Clone()
			local plot = game.Workspace:FindFirstChild(plotIdentifier)
			if plot then
				blockToPlace.Position = plot.Position + Vector3.new(0, 10, 0)
				blockToPlace.Parent = plot
				blockToPlace.Anchored = true
				print("Block placed successfully")

				if not playerBlocks[player.UserId] then
					playerBlocks[player.UserId] = {}
				end
				table.insert(playerBlocks[player.UserId], blockToPlace)
			else
				print("Invalid plot identifier")
			end
		else
			print("Block not found: " .. blockName)
		end
	end
end

-- Connect the function to handle the event
placementEvent.OnServerEvent:Connect(onBlockPlaced)

-- Function to save player blocks
local function savePlayerBlocks(player)
	local userId = player.UserId
	local blocks = playerBlocks[userId]

	if blocks then
		local blockData = {}
		for _, block in pairs(blocks) do
			table.insert(blockData, {Name = block.Name, Position = block.Position})
		end

		local success, errorMessage = pcall(function()
			myDataStore:SetAsync(userId, blockData)
		end)

		if not success then
			print("Failed to save data: ", errorMessage)
		end
	end
end

-- Function to restore player blocks
local function restoreBlocks(player)
	local userId = player.UserId

	local success, blockData = pcall(function()
		return myDataStore:GetAsync(userId)
	end)

	if success and blockData then
		for _, data in pairs(blockData) do
			local blockToPlace = ReplicatedStorage:FindFirstChild("Builder"):FindFirstChild(data.Name):Clone()
			if blockToPlace then
				blockToPlace.Position = data.Position
				blockToPlace.Parent = game.Workspace:FindFirstChild(player.Name .. "_Plot")
				blockToPlace.Anchored = true
			end
		end
	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)

Sorry, that’s how blind guessing goes. Not enough to work with here without guess re-wright and guess creating everything.

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