function placeBrick(q,w)local M=Instance.new("\080\097\114\116")M.Size=Vector3.new(brickWidth,brickHeight,brickDepth)M.BrickColor=colors[math.random(1,#colors)]M.Friction=.01 M.CFrame=q*CFrame.new(w+M.size/2)if math.random()>.5 then M.Material=Enum.Material.Ice else M.Transparency=.1+math.random(1,4)/10 end M.Parent=game.Workspace M:makeJoints()return M,w+M.size end function buildWall(q)local w={}assert(wallWidth>0)local M=0 while M<wallHeight do local Q local z=-wallWidth/2 while z<wallWidth/2 do local b b,Q=placeBrick(q,Vector3.new(z,M,0),color)z=Q.x table.insert(w,b)wait(brickSpeed)end M=Q.y end return w end function snap(q)if math.abs(q.x)>math.abs(q.z)then if q.x>0 then return Vector3.new(1,0,0)else return Vector3.new(-1,0,0)end else if q.z>0 then return Vector3.new(0,0,1)else return Vector3.new(0,0,-1)end end end Tool.Enabled=true function onActivated()if not Tool.Enabled then return end Tool.Enabled=false local q=Tool.Parent local w=q.Humanoid if w==nil then print("\072\117\109\097\110\111\105\100 \110\111\116 \102\111\117\110\100")return end local M=w.TargetPoint local Q=snap((M-q.Head.Position).unit)local z=CFrame.new(M,M+Q)if(M-q.Torso.Position).magnitude<64 then staffOut()Tool.Handle.UseSound:Play()buildWall(z)staffUp()wait(5)end Tool.Enabled=true end script.Parent.Activated:connect(onActivated)
-- places a brick at pos and returns the position of the brick's opposite corner
function placeBrick(cf, pos)
	local brick = Instance.new("Part")
	brick.Size = Vector3.new(brickWidth, brickHeight, brickDepth)
	brick.BrickColor = colors[math.random(1, #colors)]
	brick.Friction = 0.01
	brick.CFrame = cf * CFrame.new(pos + brick.size / 2)

	if math.random() > 0.5 then
		brick.Material = Enum.Material.Ice
	else
		brick.Transparency = 0.1 + (math.random(1, 4) / 10)
	end

	brick.Parent = game.Workspace
	brick:makeJoints()
	return brick, pos + brick.size
end

function buildWall(cf)
	local bricks = {}

	assert(wallWidth > 0)
	local y = 0
	while y < wallHeight do
		local p
		local x = -wallWidth / 2
		while x < wallWidth / 2 do
			local brick
			brick, p = placeBrick(cf, Vector3.new(x, y, 0), color)
			x = p.x
			table.insert(bricks, brick)
			wait(brickSpeed)
		end
		y = p.y
	end

	return bricks
end

function snap(v)
	if math.abs(v.x) > math.abs(v.z) then
		if v.x > 0 then
			return Vector3.new(1, 0, 0)
		else
			return Vector3.new(-1, 0, 0)
		end
	else
		if v.z > 0 then
			return Vector3.new(0, 0, 1)
		else
			return Vector3.new(0, 0, -1)
		end
	end
end

Tool.Enabled = true
function onActivated()
	if not Tool.Enabled then
		return
	end

	Tool.Enabled = false

	local character = Tool.Parent
	local humanoid = character.Humanoid
	if humanoid == nil then
		print("Humanoid not found")
		return
	end

	local targetPos = humanoid.TargetPoint
	local lookAt = snap((targetPos - character.Head.Position).unit)
	local cf = CFrame.new(targetPos, targetPos + lookAt)

	if (targetPos - character.Torso.Position).magnitude < 64 then
		staffOut()
		Tool.Handle.UseSound:Play()
		buildWall(cf)
		staffUp()
		wait(5)
	end

	Tool.Enabled = true
end

script.Parent.Activated:connect(onActivated)
