Stacking system not working

Hi guys. I want to create a game where players need to stack parts to build a tower. like in the mobile game stack. but I don’t know why my system is not working

somtimes the block is moving and than its not moving and the placement is not working. please can someone help me

Script in MovingBlock

local block = script.Parent
local direction = Vector3.new(1, 0, 0) -- Bewegung entlang der X-Achse
local speed = 10 -- Geschwindigkeit

-- Bewegung starten
while true do
	block.Position = block.Position + direction * speed * wait()
	if block.Position.X > 10 or block.Position.X < -10 then
		direction = direction * -1 -- Richtungswechsel
	end
end

StarterPlayerCharacterScripts (Stacking Script)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

-- Funktion zur Überprüfung der Genauigkeit und Anpassung der Größe
local function checkAccuracy(oldBlock, newBlock)
	local overlap = math.abs(oldBlock.Position.X - newBlock.Position.X)
	if overlap > oldBlock.Size.X then
		newBlock:Destroy() -- Block löschen, wenn er komplett daneben liegt
		return false
	else
		-- Neue Größe und Position des Blocks anpassen
		local newSize = oldBlock.Size.X - overlap
		newBlock.Size = Vector3.new(newSize, newBlock.Size.Y, newBlock.Size.Z)
		newBlock.Position = Vector3.new(
			(oldBlock.Position.X + newBlock.Position.X) / 2, -- Block zentrieren
			newBlock.Position.Y,
			newBlock.Position.Z
		)
		return true
	end
end

-- Funktion zum Erstellen eines neuen Blocks
local function createNewMovingBlock(lastBlock)
	local newBlock = lastBlock:Clone() -- Kopiere den letzten Block
	newBlock.Anchored = false -- Bewegung erlauben
	newBlock.Position = lastBlock.Position + Vector3.new(0, 1, 0) -- Stapel nach oben
	newBlock.Parent = workspace -- Füge den Block zur Szene hinzu
	newBlock.Name = "MovingBlock"
	return newBlock
end

-- Funktion zum Starten der Bewegung
local function startMoving(block)
	local direction = Vector3.new(1, 0, 0) -- Bewegung entlang der X-Achse
	local speed = 10

	-- Bewegungslogik
	coroutine.wrap(function()
		while block and block.Anchored == false do
			block.Position = block.Position + direction * speed * wait()
			if block.Position.X > 10 or block.Position.X < -10 then
				direction = direction * -1 -- Richtungswechsel
			end
		end
	end)()
end

-- Hauptlogik bei Mausklick
mouse.Button1Down:Connect(function()
	local currentBlock = workspace:FindFirstChild("MovingBlock")
	if currentBlock then
		-- Bewegung stoppen
		currentBlock.Anchored = true
		currentBlock.Name = "StackedBlock"

		-- Überprüfe die Genauigkeit
		local previousBlock = nil
		for _, block in ipairs(workspace:GetChildren()) do
			if block.Name == "StackedBlock" and block.Position.Y < currentBlock.Position.Y then
				previousBlock = block
			end
		end

		if previousBlock then
			local success = checkAccuracy(previousBlock, currentBlock)
			if not success then
				print("Spiel vorbei!")
				return
			end
		end

		-- Neuen beweglichen Block erstellen
		local newBlock = createNewMovingBlock(currentBlock)
		startMoving(newBlock)
	end
end)