-- places a brick at pos and returns the position of the brick's opposite corner

function placeBrick(cf, pos)
	
	local troll = game.ServerStorage.TrollFruit:clone()
	local brick = troll.Handle
	
	local clean = script.Parent.BrickCleanup:clone()
	clean.Parent = brick
	
	brick.CFrame = cf * CFrame.new(pos)

	
	troll.Parent = game.Workspace
	
	local a = math.random(1,2)
	local b = math.random(1,2)
	local c = math.random(1,2)

	brick.Mesh.Scale = Vector3.new(a,b,c)
	
	brick.BrickColor = BrickColor.random()
	
	return  brick, pos
	
end
function buildWall(cf)

	local color = BrickColor.random()
	local bricks = {}
	
	
			local brick
			brick, p = placeBrick(cf, Vector3.new(x, y, 0), color)
			
			x = p.x
			table.insert(bricks, brick)
			wait(brickSpeed)
		
		y = p.y
	

	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 x1 = math.random(1,2)
	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 x1 == 1 then
	Tool.Handle.BuildSound:Play()

	buildWall(cf)

	wait(1.2)
Tool.Handle.BuildSound:Stop()
wait(4)
	Tool.Enabled = true
end
if x1 == 2 then
	Tool.Handle.BuildSound2:Play()

	buildWall(cf)

	wait(1.2)
Tool.Handle.BuildSound2:Stop()
wait(4)
	Tool.Enabled = true
end

end

script.Parent.Activated:connect(onActivated)

